chore(go): audit providers kimi grok qwen letta

Move Kimi, Grok, and Qwen onto managed_runtime.go and shell_command.go;
share the encoded and shell command builders and the quoted Windows
legacy parser with Droid; split managed_changes.go; dedupe Letta's hook
entry filtering and stop swallowing settings read errors.

Claude-Session: https://claude.ai/code/session_01Agoc9bRVE9KL7mmWtey9Vq
This commit is contained in:
雷电芽衣 committed 2026-09-03 02:16:58 -04:00
1 parent 1ede03d989
commit 34ff4c1ea8
34 files changed
+421 -1876

No files matched your search

+44 -122
View File
@@ -1,16 +1,16 @@
package plugins
import (
"errors"
"fmt"
"os"
"path/filepath"
)
// LettaPlugin manages the Elydora audit hook for Letta Code.
// It merges PreToolUse/PostToolUse hooks into ~/.letta/settings.json.
// LettaPlugin merges Elydora hooks into ~/.letta/settings.json.
type LettaPlugin struct{}
func (p *LettaPlugin) configPath() (string, error) {
func lettaConfigPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("resolve home directory: %w", err)
@@ -18,6 +18,16 @@ func (p *LettaPlugin) configPath() (string, error) {
return filepath.Join(home, ".letta", "settings.json"), nil
}
func lettaHookEntry(scriptPath string) map[string]any {
return map[string]any{
"matcher": "*",
"hooks": []any{map[string]any{
"type": "command",
"command": "node " + scriptPath,
}},
}
}
func (p *LettaPlugin) Install(config InstallConfig) error {
scriptPath, err := hookScriptPath(config.AgentID)
if err != nil {
@@ -26,11 +36,9 @@ func (p *LettaPlugin) Install(config InstallConfig) error {
if config.HookScript != "" {
scriptPath = config.HookScript
}
if err := GenerateHookScript(scriptPath, config); err != nil {
return fmt.Errorf("generate hook script: %w", err)
}
guardPath := config.GuardScriptPath
if guardPath == "" {
guardPath, err = guardScriptPath(config.AgentID)
@@ -38,70 +46,21 @@ func (p *LettaPlugin) Install(config InstallConfig) error {
return err
}
}
configPath, err := p.configPath()
configPath, err := lettaConfigPath()
if err != nil {
return err
}
settings, err := readJSONFile(configPath)
if err != nil {
return err
}
hooks, _ := settings["hooks"].(map[string]interface{})
hooks, _ := settings["hooks"].(map[string]any)
if hooks == nil {
hooks = make(map[string]interface{})
hooks = map[string]any{}
}
// --- PreToolUse (guard — freeze enforcement) ---
preToolUse, _ := hooks["PreToolUse"].([]interface{})
var preFiltered []interface{}
for _, entry := range preToolUse {
if m, ok := entry.(map[string]interface{}); ok {
if isElydoraHookEntry(m) {
continue
}
}
preFiltered = append(preFiltered, entry)
}
guardEntry := map[string]interface{}{
"matcher": "*",
"hooks": []interface{}{
map[string]interface{}{
"type": "command",
"command": "node " + guardPath,
},
},
}
preFiltered = append(preFiltered, guardEntry)
hooks["PreToolUse"] = preFiltered
// --- PostToolUse (audit logging) ---
postToolUse, _ := hooks["PostToolUse"].([]interface{})
var postFiltered []interface{}
for _, entry := range postToolUse {
if m, ok := entry.(map[string]interface{}); ok {
if isElydoraHookEntry(m) {
continue
}
}
postFiltered = append(postFiltered, entry)
}
hookEntry := map[string]interface{}{
"matcher": "*",
"hooks": []interface{}{
map[string]interface{}{
"type": "command",
"command": "node " + scriptPath,
},
},
}
postFiltered = append(postFiltered, hookEntry)
hooks["PostToolUse"] = postFiltered
hooks["PreToolUse"] = append(withoutElydoraEntries(hooks["PreToolUse"]), lettaHookEntry(guardPath))
hooks["PostToolUse"] = append(withoutElydoraEntries(hooks["PostToolUse"]), lettaHookEntry(scriptPath))
settings["hooks"] = hooks
if err := writeJSONFile(configPath, settings); err != nil {
return err
}
@@ -110,74 +69,44 @@ func (p *LettaPlugin) Install(config InstallConfig) error {
}
func (p *LettaPlugin) Uninstall(agentID string) error {
configPath, err := p.configPath()
configPath, err := lettaConfigPath()
if err != nil {
return err
}
settings, err := readJSONFile(configPath)
if err != nil {
return err
}
hooks, _ := settings["hooks"].(map[string]interface{})
hooks, _ := settings["hooks"].(map[string]any)
if hooks == nil {
fmt.Println("No Letta Code hooks found.")
return nil
}
// Remove PreToolUse Elydora entries
preToolUse, _ := hooks["PreToolUse"].([]interface{})
var preFiltered []interface{}
for _, entry := range preToolUse {
if m, ok := entry.(map[string]interface{}); ok {
if isElydoraHookEntry(m) {
continue
}
for _, event := range []string{"PreToolUse", "PostToolUse"} {
kept := withoutElydoraEntries(hooks[event])
if len(kept) == 0 {
delete(hooks, event)
} else {
hooks[event] = kept
}
preFiltered = append(preFiltered, entry)
}
if len(preFiltered) == 0 {
delete(hooks, "PreToolUse")
} else {
hooks["PreToolUse"] = preFiltered
}
// Remove PostToolUse Elydora entries
postToolUse, _ := hooks["PostToolUse"].([]interface{})
var postFiltered []interface{}
for _, entry := range postToolUse {
if m, ok := entry.(map[string]interface{}); ok {
if isElydoraHookEntry(m) {
continue
}
}
postFiltered = append(postFiltered, entry)
}
if len(postFiltered) == 0 {
delete(hooks, "PostToolUse")
} else {
hooks["PostToolUse"] = postFiltered
}
if len(hooks) == 0 {
delete(settings, "hooks")
} else {
settings["hooks"] = hooks
}
if err := writeJSONFile(configPath, settings); err != nil {
return err
}
if agentID != "" {
scriptPath, _ := hookScriptPath(agentID)
if scriptPath != "" {
os.Remove(scriptPath)
}
gPath, _ := guardScriptPath(agentID)
if gPath != "" {
os.Remove(gPath)
for _, resolve := range []func(string) (string, error){hookScriptPath, guardScriptPath} {
path, err := resolve(agentID)
if err != nil {
return err
}
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("remove %s: %w", path, err)
}
}
}
fmt.Println("Uninstalled Elydora hook for Letta Code.")
@@ -185,37 +114,30 @@ func (p *LettaPlugin) Uninstall(agentID string) error {
}
func (p *LettaPlugin) Status() (PluginStatus, error) {
configPath, err := p.configPath()
configPath, err := lettaConfigPath()
if err != nil {
return PluginStatus{}, err
}
status := PluginStatus{
AgentName: "letta",
DisplayName: "Letta Code",
ConfigPath: configPath,
}
settings, err := readJSONFile(configPath)
if err != nil {
return status, err
}
hooks, _ := settings["hooks"].(map[string]any)
if hooks == nil {
return status, nil
}
hooks, _ := settings["hooks"].(map[string]interface{})
if hooks != nil {
preConfigured := hasElydoraEntry(hooks["PreToolUse"])
postConfigured := hasElydoraEntry(hooks["PostToolUse"])
status.HookConfigured = preConfigured && postConfigured
// Extract hook script path from the configured command
scriptPath := extractElydoraScriptPath(hooks["PostToolUse"])
if scriptPath != "" {
if _, err := os.Stat(scriptPath); err == nil {
status.HookScriptExists = true
}
status.HookConfigured = hasElydoraEntry(hooks["PreToolUse"]) &&
hasElydoraEntry(hooks["PostToolUse"])
if scriptPath := extractElydoraScriptPath(hooks["PostToolUse"]); scriptPath != "" {
if _, err := os.Stat(scriptPath); err == nil {
status.HookScriptExists = true
}
}
status.Installed = status.HookConfigured && status.HookScriptExists
return status, nil
}