Files
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

474 lines
15 KiB
Go

package agent
import (
"bytes"
"context"
"log/slog"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
type extensionComposerProfileResolverStub struct {
profile ExtensionComposerProfile
}
func (s extensionComposerProfileResolverStub) ResolveExtensionComposerProfile(context.Context, string) (ExtensionComposerProfile, error) {
return s.profile, nil
}
func TestDiscoverComposerSkillOptionsUsesExtensionDeclaredRoots(t *testing.T) {
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
repoDir := filepath.Join(tempDir, "repo")
cwd := filepath.Join(repoDir, "packages", "app")
t.Setenv("HOME", homeDir)
t.Setenv("USERPROFILE", homeDir)
writeSkill(t, filepath.Join(repoDir, ".gemini", "skills", "project-review", "SKILL.md"), `---
name: project-review
description: Review this project.
---
`)
writeSkill(t, filepath.Join(homeDir, ".agents", "skills", "personal-review", "SKILL.md"), `---
name: personal-review
description: Review any project.
---
`)
service := newIsolatedAgentService(newFakeRuntime())
service.ExtensionComposerProfiles = extensionComposerProfileResolverStub{
profile: ExtensionComposerProfile{Skills: &ExtensionComposerSkillProfile{
Invocation: "textTrigger",
TriggerPrefix: "/skill:",
Roots: []ExtensionComposerSkillRoot{
{Scope: "workspace", Path: ".gemini/skills"},
{Scope: "user", Path: ".agents/skills"},
},
}},
}
options := service.discoverComposerSkillOptionsForLaunch(
context.Background(),
"acp:gemini",
cwd,
nil,
map[string]any{"kind": "agent_extension", "extensionInstallationId": "gemini@1.0.1"},
)
if got := composerSkillOptionTriggers(options); !slices.Equal(got, []string{"/skill:project-review", "/skill:personal-review"}) {
t.Fatalf("extension skill triggers = %#v", got)
}
for _, option := range options {
if option.Invocation != "textTrigger" {
t.Fatalf("extension skill invocation = %q", option.Invocation)
}
}
}
func TestDiscoverComposerSkillOptionsForExtensionHidesTuttiInjectedSkills(t *testing.T) {
tempDir := t.TempDir()
repoDir := filepath.Join(tempDir, "repo")
cwd := filepath.Join(repoDir, "packages", "app")
writeSkill(t, filepath.Join(repoDir, ".agent_context", "skills", "browser-use", "SKILL.md"), `---
name: browser-use
description: Use Tutti browser automation.
---
`)
writeSkill(t, filepath.Join(repoDir, ".agent_context", "skills", "computer-use", "SKILL.md"), `---
name: computer-use
description: Use Tutti computer automation.
---
`)
writeSkill(t, filepath.Join(repoDir, ".agent_context", "skills", "hermes-native", "SKILL.md"), `---
name: hermes-native
description: Native Hermes skill.
---
`)
service := newIsolatedAgentService(newFakeRuntime())
service.ExtensionComposerProfiles = extensionComposerProfileResolverStub{
profile: ExtensionComposerProfile{Skills: &ExtensionComposerSkillProfile{
Invocation: "textTrigger",
TriggerPrefix: "/",
Roots: []ExtensionComposerSkillRoot{
{Scope: "workspace", Path: ".agent_context/skills"},
},
}},
}
options := service.discoverComposerSkillOptionsForLaunch(
context.Background(),
"acp:hermes",
cwd,
nil,
map[string]any{"kind": "agent_extension", "extensionInstallationId": "hermes@0.1.0"},
)
if got := composerSkillOptionTriggers(options); !slices.Equal(got, []string{"/hermes-native"}) {
t.Fatalf("extension skill triggers = %#v, want native Hermes skills without Tutti-injected browser-use or computer-use", got)
}
}
func TestDiscoverComposerSkillOptionsCodexUsesProviderNativeTriggers(t *testing.T) {
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
repoDir := filepath.Join(tempDir, "repo")
cwd := filepath.Join(repoDir, "packages", "app")
codexHome := filepath.Join(tempDir, "runtime", "codex-home")
t.Setenv("HOME", homeDir)
t.Setenv("USERPROFILE", homeDir)
writeSkill(t, filepath.Join(repoDir, ".codex", "skills", "architecture-review", "SKILL.md"), `---
description: Review architecture changes.
---
Review repository changes.
`)
writeSkill(t, filepath.Join(homeDir, ".agents", "skills", "lark-doc", "SKILL.md"), `---
description: >
Work with Lark documents.
Search and edit cloud docs.
---
`)
writeSkill(t, filepath.Join(homeDir, ".agents", "skills", "broken-agents", "SKILL.md"), `description: Missing frontmatter delimiter.
---
`)
writeSkill(t, filepath.Join(homeDir, ".codex", "skills", "caveman", "SKILL.md"), `---
description: >
Ultra-compressed communication mode.
Use when the user asks to be brief.
---
`)
writeSkill(t, filepath.Join(homeDir, ".codex", "skills", "broken-codex", "SKILL.md"), `---
description: Missing closing delimiter.
`)
writeSkill(t, filepath.Join(homeDir, ".codex", "skills", ".system", "hidden", "SKILL.md"), `---
description: Hidden system skill.
---
`)
writeSkill(t, filepath.Join(codexHome, "skills", ".system", "imagegen", "SKILL.md"), `---
description: Generate images.
---
`)
writeSkill(t, filepath.Join(codexHome, "skills", "tutti-cli", "SKILL.md"), `---
description: Internal Tutti CLI.
---
`)
writeSkill(t, filepath.Join(codexHome, "skills", "music-lookup", "SKILL.md"), `---
description: Look up music.
---
`)
managedSkillDir := filepath.Join(codexHome, "skills", "managed-extra")
writeSkill(t, filepath.Join(managedSkillDir, "SKILL.md"), `---
description: Managed runtime skill.
---
`)
if err := os.WriteFile(filepath.Join(managedSkillDir, ".tutti-managed-skill"), nil, 0o600); err != nil {
t.Fatalf("write managed skill marker: %v", err)
}
options := discoverComposerSkillOptions("codex", cwd, []string{
"CODEX_HOME=" + codexHome,
})
triggers := composerSkillOptionTriggers(options)
wantOrdered := []string{"$architecture-review", "$caveman", "$lark-doc", "$music-lookup", "$imagegen"}
if !containsStringsInOrder(triggers, wantOrdered) {
t.Fatalf("triggers = %#v, want ordered subset %#v", triggers, wantOrdered)
}
optionsByTrigger := make(map[string]ComposerSkillOption, len(options))
for _, option := range options {
optionsByTrigger[option.Trigger] = option
}
if optionsByTrigger["$architecture-review"].SourceKind != "project" || optionsByTrigger["$caveman"].SourceKind != "personal" || optionsByTrigger["$lark-doc"].SourceKind != "personal" || optionsByTrigger["$music-lookup"].SourceKind != "personal" || optionsByTrigger["$imagegen"].SourceKind != "system" {
t.Fatalf("source kinds = %#v", options)
}
if optionsByTrigger["$caveman"].Description != "Ultra-compressed communication mode. Use when the user asks to be brief." {
t.Fatalf("codex personal description = %q", optionsByTrigger["$caveman"].Description)
}
if optionsByTrigger["$lark-doc"].Description != "Work with Lark documents. Search and edit cloud docs." {
t.Fatalf("folded description = %q", optionsByTrigger["$lark-doc"].Description)
}
}
func TestDiscoverComposerSkillOptionsClaudeUsesSlashAndPluginNamespace(t *testing.T) {
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
repoDir := filepath.Join(tempDir, "repo")
cwd := filepath.Join(repoDir, "apps", "desktop")
pluginDir := filepath.Join(tempDir, "plugins", "product-design")
t.Setenv("HOME", homeDir)
t.Setenv("USERPROFILE", homeDir)
writeSkill(t, filepath.Join(repoDir, ".claude", "skills", "summarize", "SKILL.md"), `---
description: Summarize changes.
---
`)
writeSkill(t, filepath.Join(homeDir, ".claude", "skills", "personal-review", "SKILL.md"), `---
description: Review personal workflow.
---
`)
writeSkill(t, filepath.Join(pluginDir, "skills", "frontend-design", "SKILL.md"), `---
description: Design frontend UI.
---
`)
writeSkill(t, filepath.Join(pluginDir, "skills", "tutti-cli", "SKILL.md"), `---
description: Internal Tutti CLI.
---
`)
options := discoverComposerSkillOptions("claude-code", cwd, []string{
"TUTTI_CLAUDE_PLUGIN_DIR=" + pluginDir,
})
triggers := composerSkillOptionTriggers(options)
want := []string{"/summarize", "/personal-review", "/product-design:frontend-design"}
if !equalStringSlices(triggers, want) {
t.Fatalf("triggers = %#v, want %#v", triggers, want)
}
if options[2].PluginName != "product-design" || options[2].SourceKind != "plugin" {
t.Fatalf("plugin option = %#v", options[2])
}
}
func TestDiscoverComposerSkillOptionsCursorUsesPluginDir(t *testing.T) {
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
repoDir := filepath.Join(tempDir, "repo")
cwd := filepath.Join(repoDir, "apps", "desktop")
pluginDir := filepath.Join(tempDir, "plugins", "tutti-cli")
t.Setenv("HOME", homeDir)
t.Setenv("USERPROFILE", homeDir)
writeSkill(t, filepath.Join(repoDir, ".cursor", "skills", "project-skill", "SKILL.md"), `---
description: Project Cursor skill.
---
`)
writeSkill(t, filepath.Join(homeDir, ".cursor", "skills", "personal-skill", "SKILL.md"), `---
description: Personal Cursor skill.
---
`)
writeSkill(t, filepath.Join(pluginDir, "skills", "workflow-check", "SKILL.md"), `---
description: Runtime Cursor plugin skill.
---
`)
writeSkill(t, filepath.Join(pluginDir, "skills", "tutti-cli", "SKILL.md"), `---
description: Internal Tutti CLI.
---
`)
options := discoverComposerSkillOptions("cursor", cwd, []string{
"TUTTI_CURSOR_PLUGIN_DIR=" + pluginDir,
})
triggers := composerSkillOptionTriggers(options)
want := []string{"$project-skill", "$personal-skill", "$workflow-check"}
if !equalStringSlices(triggers, want) {
t.Fatalf("triggers = %#v, want %#v", triggers, want)
}
if options[2].PluginName != "tutti-cli" || options[2].SourceKind != "plugin" {
t.Fatalf("plugin option = %#v", options[2])
}
}
func TestDiscoverComposerSkillOptionsOpenCodeUsesNativeAndCompatibleRoots(t *testing.T) {
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
repoDir := filepath.Join(tempDir, "repo")
cwd := filepath.Join(repoDir, "apps", "desktop")
customConfigDir := filepath.Join(tempDir, "opencode-config")
t.Setenv("HOME", homeDir)
t.Setenv("USERPROFILE", homeDir)
writeSkill(t, filepath.Join(repoDir, ".opencode", "skills", "project-open", "SKILL.md"), `---
description: Project OpenCode skill.
---
`)
writeSkill(t, filepath.Join(repoDir, ".claude", "skills", "project-claude", "SKILL.md"), `---
description: Claude-compatible OpenCode skill.
---
`)
writeSkill(t, filepath.Join(repoDir, ".agents", "skills", "project-agent", "SKILL.md"), `---
description: Agent-compatible OpenCode skill.
---
`)
writeSkill(t, filepath.Join(homeDir, ".config", "opencode", "skills", "personal-open", "SKILL.md"), `---
description: Personal OpenCode skill.
---
`)
writeSkill(t, filepath.Join(homeDir, ".claude", "skills", "personal-claude", "SKILL.md"), `---
description: Personal Claude-compatible skill.
---
`)
writeSkill(t, filepath.Join(homeDir, ".agents", "skills", "personal-agent", "SKILL.md"), `---
description: Personal agent-compatible skill.
---
`)
writeSkill(t, filepath.Join(customConfigDir, "opencode", "skills", "custom-config", "SKILL.md"), `---
description: Custom OpenCode config skill.
---
`)
options := discoverComposerSkillOptions("opencode", cwd, []string{
"OPENCODE_CONFIG_DIR=" + customConfigDir,
})
triggers := composerSkillOptionTriggers(options)
want := []string{
"/project-agent",
"/project-claude",
"/project-open",
"/custom-config",
"/personal-agent",
"/personal-claude",
"/personal-open",
}
if !equalStringSlices(triggers, want) {
t.Fatalf("triggers = %#v, want %#v", triggers, want)
}
if options[0].SourceKind != "project" || options[3].SourceKind != "personal" {
t.Fatalf("source kinds = %#v", options)
}
}
func TestDiscoverComposerSkillOptionsWarnsOnceForUnchangedInvalidSkill(t *testing.T) {
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
cwd := filepath.Join(tempDir, "repo")
t.Setenv("HOME", homeDir)
t.Setenv("USERPROFILE", homeDir)
skillPath := filepath.Join(homeDir, ".codex", "skills", "broken", "SKILL.md")
writeSkill(t, skillPath, `description: Missing frontmatter delimiter.
---
`)
skillMetadataCache.mu.Lock()
skillMetadataCache.entries = make(map[string]skillMetadataCacheEntry)
skillMetadataCache.mu.Unlock()
var output bytes.Buffer
previousLogger := slog.Default()
slog.SetDefault(slog.New(slog.NewTextHandler(&output, nil)))
t.Cleanup(func() {
slog.SetDefault(previousLogger)
})
_ = discoverComposerSkillOptions("codex", cwd, nil)
_ = discoverComposerSkillOptions("codex", cwd, nil)
if count := strings.Count(output.String(), "skill_frontmatter_invalid"); count != 1 {
t.Fatalf("invalid frontmatter warnings = %d, want 1; output:\n%s", count, output.String())
}
writeSkill(t, skillPath, `description: Still missing frontmatter delimiter.
---
`)
modTime := time.Now().Add(2 * time.Second)
if err := os.Chtimes(skillPath, modTime, modTime); err != nil {
t.Fatalf("Chtimes: %v", err)
}
_ = discoverComposerSkillOptions("codex", cwd, nil)
if count := strings.Count(output.String(), "skill_frontmatter_invalid"); count != 2 {
t.Fatalf("invalid frontmatter warnings after modification = %d, want 2; output:\n%s", count, output.String())
}
}
func TestReadSkillMetadataSupportsFoldedDescription(t *testing.T) {
path := filepath.Join(t.TempDir(), "SKILL.md")
writeSkill(t, path, `---
name: lark-whiteboard
version: 1.0.0
description: >
飞书画板:查询和编辑飞书云文档中的画板。
支持导出画板为预览图片、导出原始节点结构。
metadata:
requires:
bins: ["lark-cli"]
---
`)
metadata, ok := readSkillMetadata(path)
if !ok {
t.Fatalf("readSkillMetadata() ok = false, want true")
}
if metadata.name != "lark-whiteboard" {
t.Fatalf("name = %q", metadata.name)
}
want := "飞书画板:查询和编辑飞书云文档中的画板。 支持导出画板为预览图片、导出原始节点结构。"
if metadata.description != want {
t.Fatalf("description = %q, want %q", metadata.description, want)
}
}
func TestReadSkillMetadataRejectsMissingDelimitedFrontmatter(t *testing.T) {
tempDir := t.TempDir()
tests := []struct {
name string
content string
}{
{
name: "missing start delimiter",
content: `name: broken
---
`,
},
{
name: "missing end delimiter",
content: `---
name: broken
`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
path := filepath.Join(tempDir, test.name, "SKILL.md")
writeSkill(t, path, test.content)
metadata, ok := readSkillMetadata(path)
if ok {
t.Fatalf("readSkillMetadata() ok = true, want false")
}
if metadata.name != "" || metadata.description != "" {
t.Fatalf("metadata = %#v, want empty", metadata)
}
})
}
}
func writeSkill(t *testing.T, path string, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("MkdirAll: %v", err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
}
func composerSkillOptionTriggers(options []ComposerSkillOption) []string {
triggers := make([]string, 0, len(options))
for _, option := range options {
triggers = append(triggers, option.Trigger)
}
return triggers
}
func equalStringSlices(left []string, right []string) bool {
if len(left) != len(right) {
return false
}
for index := range left {
if left[index] != right[index] {
return false
}
}
return true
}
func containsStringsInOrder(values []string, ordered []string) bool {
next := 0
for _, value := range values {
if next < len(ordered) && value == ordered[next] {
next++
}
}
return next == len(ordered)
}