fix(droid): align hooks with Factory 0.175
This commit is contained in:
1 parent
827374e7f8
commit
4521cbe32f
15 files changed
+2749
-881
No files matched your search
@@ -6,33 +6,26 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
droidAgentKey = "droid"
|
||||
droidGuardScript = "guard.js"
|
||||
droidAuditScript = "hook.js"
|
||||
droidHookTimeout = float64(10)
|
||||
droidOwnedFileMarker = "// Managed by Elydora"
|
||||
droidPOSIXApostrophe = `'"'"'`
|
||||
droidAgentKey = "droid"
|
||||
droidGuardScript = "guard.js"
|
||||
droidAuditScript = "hook.js"
|
||||
droidHookTimeout = float64(10)
|
||||
droidOwnedFileMarker = "// Managed by Elydora"
|
||||
droidPOSIXApostrophe = `'"'"'`
|
||||
droidWindowsExitSuffix = "; exit $LASTEXITCODE"
|
||||
)
|
||||
|
||||
var droidToolEvents = [...]string{"PreToolUse", "PostToolUse"}
|
||||
|
||||
var droidEventNames = map[string]bool{
|
||||
"PreToolUse": true, "PostToolUse": true, "Notification": true,
|
||||
"UserPromptSubmit": true, "Stop": true, "SubagentStop": true,
|
||||
"PreCompact": true, "SessionStart": true, "SessionEnd": true,
|
||||
}
|
||||
|
||||
var droidFlagNames = map[string]bool{"hooksDisabled": true, "showHookOutput": true}
|
||||
|
||||
type droidHookSettings map[string]any
|
||||
|
||||
type droidManagedRemoval struct {
|
||||
@@ -58,30 +51,20 @@ func readDroidHookSettings(value any, label string) (droidHookSettings, error) {
|
||||
if !ok || object == nil {
|
||||
return nil, fmt.Errorf("%s must contain a JSON object", label)
|
||||
}
|
||||
settings := make(droidHookSettings, len(object))
|
||||
for key, item := range object {
|
||||
if droidFlagNames[key] {
|
||||
if _, ok := item.(bool); !ok {
|
||||
return nil, fmt.Errorf(`%s field %q must be a boolean`, label, key)
|
||||
}
|
||||
settings[key] = item
|
||||
continue
|
||||
}
|
||||
if !droidEventNames[key] {
|
||||
return nil, fmt.Errorf(`%s contains unsupported field %q`, label, key)
|
||||
}
|
||||
hooks := make(droidHookSettings, len(object))
|
||||
for event, item := range object {
|
||||
groups, ok := item.([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(`%s field %q must be an array`, label, key)
|
||||
return nil, fmt.Errorf(`%s field %q must be an array`, label, event)
|
||||
}
|
||||
for groupIndex, group := range groups {
|
||||
if err := validateDroidGroup(group, label, key, groupIndex); err != nil {
|
||||
if err := validateDroidGroup(group, label, event, groupIndex); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
settings[key] = item
|
||||
hooks[event] = item
|
||||
}
|
||||
return settings, nil
|
||||
return hooks, nil
|
||||
}
|
||||
|
||||
func validateDroidGroup(value any, label, event string, groupIndex int) error {
|
||||
@@ -121,13 +104,14 @@ func validateDroidHandler(value any, groupLabel string, handlerIndex int) error
|
||||
if handler["type"] != "command" {
|
||||
return fmt.Errorf(`%s type must be "command"`, location)
|
||||
}
|
||||
if _, ok := handler["command"].(string); !ok {
|
||||
return fmt.Errorf("%s command must be a string", location)
|
||||
command, ok := handler["command"].(string)
|
||||
if !ok || strings.TrimSpace(command) == "" {
|
||||
return fmt.Errorf("%s command must be a non-empty string", location)
|
||||
}
|
||||
if timeout, exists := handler["timeout"]; exists {
|
||||
number, ok := timeout.(float64)
|
||||
if !ok || math.IsNaN(number) || math.IsInf(number, 0) {
|
||||
return fmt.Errorf("%s timeout must be a finite number", location)
|
||||
if !ok || math.IsNaN(number) || math.IsInf(number, 0) || number <= 0 {
|
||||
return fmt.Errorf("%s timeout must be a positive finite number", location)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -137,19 +121,19 @@ func validateDroidRegexes(settings ...droidHookSettings) error {
|
||||
entries := make([]droidRegexEntry, 0)
|
||||
for _, source := range settings {
|
||||
for event, value := range source {
|
||||
if droidFlagNames[event] {
|
||||
continue
|
||||
}
|
||||
for index, groupValue := range value.([]any) {
|
||||
groups, _ := value.([]any)
|
||||
for index, groupValue := range groups {
|
||||
group := groupValue.(map[string]any)
|
||||
if matcher, ok := group["matcher"].(string); ok && matcher != "" && matcher != "*" {
|
||||
entries = append(entries, droidRegexEntry{
|
||||
Label: fmt.Sprintf(`Factory Droid hooks field %q[%d] matcher`, event, index), Pattern: matcher,
|
||||
Label: fmt.Sprintf(`Factory Droid hooks field %q[%d] matcher`, event, index),
|
||||
Pattern: matcher,
|
||||
})
|
||||
}
|
||||
if expression, ok := group["commandRegex"].(string); ok {
|
||||
entries = append(entries, droidRegexEntry{
|
||||
Label: fmt.Sprintf(`Factory Droid hooks field %q[%d] commandRegex`, event, index), Pattern: expression,
|
||||
Label: fmt.Sprintf(`Factory Droid hooks field %q[%d] commandRegex`, event, index),
|
||||
Pattern: expression,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -196,17 +180,14 @@ for (const entry of entries) {
|
||||
|
||||
func buildDroidCommand(nodePath, scriptPath string) string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return quoteDroidWindowsArgument(nodePath) + " " + quoteDroidWindowsArgument(scriptPath)
|
||||
return "& " + quoteDroidPowerShellArgument(nodePath) + " " +
|
||||
quoteDroidPowerShellArgument(scriptPath) + droidWindowsExitSuffix
|
||||
}
|
||||
return quotePOSIXArgument(nodePath) + " " + quotePOSIXArgument(scriptPath)
|
||||
}
|
||||
|
||||
func quoteDroidWindowsArgument(value string) string {
|
||||
quoted := quoteWindowsArgument(value)
|
||||
if strings.HasPrefix(quoted, `"`) {
|
||||
return quoted
|
||||
}
|
||||
return `"` + quoted + `"`
|
||||
func quoteDroidPowerShellArgument(value string) string {
|
||||
return "'" + strings.ReplaceAll(value, "'", "''") + "'"
|
||||
}
|
||||
|
||||
func buildDroidGroup(nodePath, scriptPath string) map[string]any {
|
||||
@@ -218,38 +199,80 @@ func buildDroidGroup(nodePath, scriptPath string) map[string]any {
|
||||
}
|
||||
}
|
||||
|
||||
func parseDroidCommand(command string) (string, string, bool) {
|
||||
reader := readDroidPOSIXArgument
|
||||
if runtime.GOOS == "windows" {
|
||||
reader = readDroidWindowsArgument
|
||||
func parseDroidCommand(command string, includeLegacy ...bool) (string, string, bool) {
|
||||
if runtime.GOOS != "windows" {
|
||||
return parseDroidPOSIXCommand(command)
|
||||
}
|
||||
executable, next, ok := reader(command, 0)
|
||||
executable, script, ok := parseDroidPowerShellCommand(command)
|
||||
if ok || len(includeLegacy) == 0 || !includeLegacy[0] {
|
||||
return executable, script, ok
|
||||
}
|
||||
return parseDroidLegacyWindowsCommand(command)
|
||||
}
|
||||
|
||||
func parseDroidPOSIXCommand(command string) (string, string, bool) {
|
||||
executable, next, ok := readDroidPOSIXArgument(command, 0)
|
||||
if !ok || next >= len(command) || command[next] != ' ' {
|
||||
return "", "", false
|
||||
}
|
||||
script, end, ok := reader(command, next+1)
|
||||
script, end, ok := readDroidPOSIXArgument(command, next+1)
|
||||
return executable, script, ok && end == len(command) && executable != "" && script != ""
|
||||
}
|
||||
|
||||
func readDroidWindowsArgument(command string, start int) (string, int, bool) {
|
||||
if start >= len(command) || command[start] != '"' {
|
||||
func parseDroidPowerShellCommand(command string) (string, string, bool) {
|
||||
if !strings.HasPrefix(command, "& ") {
|
||||
return "", "", false
|
||||
}
|
||||
executable, next, ok := readDroidPowerShellArgument(command, 2)
|
||||
if !ok || next >= len(command) || command[next] != ' ' {
|
||||
return "", "", false
|
||||
}
|
||||
script, end, ok := readDroidPowerShellArgument(command, next+1)
|
||||
return executable, script, ok && command[end:] == droidWindowsExitSuffix && executable != "" && script != ""
|
||||
}
|
||||
|
||||
func parseDroidLegacyWindowsCommand(command string) (string, string, bool) {
|
||||
executable, next, ok := readDroidLegacyWindowsArgument(command, 0)
|
||||
if !ok || next >= len(command) || command[next] != ' ' {
|
||||
return "", "", false
|
||||
}
|
||||
script, end, ok := readDroidLegacyWindowsArgument(command, next+1)
|
||||
return executable, script, ok && end == len(command) && executable != "" && script != ""
|
||||
}
|
||||
|
||||
func readDroidPowerShellArgument(command string, start int) (string, int, bool) {
|
||||
if start >= len(command) || command[start] != '\'' {
|
||||
return "", start, false
|
||||
}
|
||||
var value strings.Builder
|
||||
for index := start + 1; index < len(command); index++ {
|
||||
if command[index] == '\\' && index+1 < len(command) && command[index+1] == '"' {
|
||||
value.WriteByte('"')
|
||||
if command[index] != '\'' {
|
||||
value.WriteByte(command[index])
|
||||
continue
|
||||
}
|
||||
if index+1 < len(command) && command[index+1] == '\'' {
|
||||
value.WriteByte('\'')
|
||||
index++
|
||||
continue
|
||||
}
|
||||
if command[index] == '"' {
|
||||
return value.String(), index + 1, true
|
||||
}
|
||||
value.WriteByte(command[index])
|
||||
return value.String(), index + 1, true
|
||||
}
|
||||
return "", start, false
|
||||
}
|
||||
|
||||
func readDroidLegacyWindowsArgument(command string, start int) (string, int, bool) {
|
||||
if start >= len(command) || command[start] != '"' {
|
||||
return "", start, false
|
||||
}
|
||||
end := strings.IndexByte(command[start+1:], '"')
|
||||
if end < 0 {
|
||||
return "", start, false
|
||||
}
|
||||
end += start + 1
|
||||
value := command[start+1 : end]
|
||||
return value, end + 1, value != "" && !strings.ContainsAny(value, "\r\n")
|
||||
}
|
||||
|
||||
func readDroidPOSIXArgument(command string, start int) (string, int, bool) {
|
||||
if start >= len(command) || command[start] != '\'' {
|
||||
return "", start, false
|
||||
@@ -293,14 +316,14 @@ func sameDroidAgentID(left, right string) bool {
|
||||
}
|
||||
|
||||
func droidRuntimeRoot() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve home directory: %w", err)
|
||||
}
|
||||
return filepath.Join(home, ".elydora"), nil
|
||||
return AgentRuntimeRoot()
|
||||
}
|
||||
|
||||
func managedDroidAgentID(handler map[string]any, scriptName, runtimeRoot string) (string, bool) {
|
||||
func managedDroidAgentID(
|
||||
handler map[string]any,
|
||||
scriptName, runtimeRoot string,
|
||||
includeLegacy ...bool,
|
||||
) (string, bool) {
|
||||
if len(handler) != 3 || handler["type"] != "command" || handler["timeout"] != droidHookTimeout {
|
||||
return "", false
|
||||
}
|
||||
@@ -308,9 +331,9 @@ func managedDroidAgentID(handler map[string]any, scriptName, runtimeRoot string)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
executable, scriptPath, ok := parseDroidCommand(command)
|
||||
if !ok || !filepath.IsAbs(executable) || !filepath.IsAbs(scriptPath) || !isDroidNodeExecutable(executable) ||
|
||||
!sameDroidFileName(filepath.Base(scriptPath), scriptName) {
|
||||
executable, scriptPath, ok := parseDroidCommand(command, includeLegacy...)
|
||||
if !ok || !filepath.IsAbs(executable) || !filepath.IsAbs(scriptPath) ||
|
||||
!isDroidNodeExecutable(executable) || !sameDroidFileName(filepath.Base(scriptPath), scriptName) {
|
||||
return "", false
|
||||
}
|
||||
agentDirectory := filepath.Dir(scriptPath)
|
||||
@@ -323,10 +346,7 @@ func managedDroidAgentID(handler map[string]any, scriptName, runtimeRoot string)
|
||||
|
||||
func isDroidNodeExecutable(path string) bool {
|
||||
name := filepath.Base(path)
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.EqualFold(name, "node.exe")
|
||||
}
|
||||
return name == "node"
|
||||
return name == "node" || strings.EqualFold(name, "node.exe")
|
||||
}
|
||||
|
||||
func sameDroidFileName(left, right string) bool {
|
||||
@@ -347,7 +367,9 @@ func managedDroidRemovals(settings droidHookSettings, agentID, runtimeRoot strin
|
||||
handlers := group["hooks"].([]any)
|
||||
indexes := make([]int, 0)
|
||||
for handlerIndex, handlerValue := range handlers {
|
||||
managedID, managed := managedDroidAgentID(handlerValue.(map[string]any), contract.script, runtimeRoot)
|
||||
managedID, managed := managedDroidAgentID(
|
||||
handlerValue.(map[string]any), contract.script, runtimeRoot, true,
|
||||
)
|
||||
if managed && (agentID == "" || sameDroidAgentID(managedID, agentID)) {
|
||||
indexes = append(indexes, handlerIndex)
|
||||
}
|
||||
@@ -357,8 +379,11 @@ func managedDroidRemovals(settings droidHookSettings, agentID, runtimeRoot strin
|
||||
}
|
||||
_, hasMatcher := group["matcher"]
|
||||
_, hasHooks := group["hooks"]
|
||||
exactGroup := len(group) == 2 && hasMatcher && hasHooks && group["matcher"] == "*" && len(indexes) == len(handlers)
|
||||
removals = append(removals, droidManagedRemoval{contract.event, groupIndex, indexes, exactGroup})
|
||||
exactGroup := len(group) == 2 && hasMatcher && hasHooks &&
|
||||
group["matcher"] == "*" && len(indexes) == len(handlers)
|
||||
removals = append(removals, droidManagedRemoval{
|
||||
contract.event, groupIndex, indexes, exactGroup,
|
||||
})
|
||||
}
|
||||
}
|
||||
return removals
|
||||
@@ -367,20 +392,24 @@ func managedDroidRemovals(settings droidHookSettings, agentID, runtimeRoot strin
|
||||
func droidRuntimeContracts(settings droidHookSettings, runtimeRoot string) []droidRuntimeContract {
|
||||
guards := managedDroidIDs(settings, "PreToolUse", droidGuardScript, runtimeRoot)
|
||||
audits := managedDroidIDs(settings, "PostToolUse", droidAuditScript, runtimeRoot)
|
||||
contracts := make([]droidRuntimeContract, 0)
|
||||
for guardID := range guards {
|
||||
matched := false
|
||||
guardIDs := make([]string, 0, len(guards))
|
||||
for agentID := range guards {
|
||||
guardIDs = append(guardIDs, agentID)
|
||||
}
|
||||
sort.Strings(guardIDs)
|
||||
contracts := make([]droidRuntimeContract, 0, len(guardIDs))
|
||||
for _, guardID := range guardIDs {
|
||||
for auditID := range audits {
|
||||
if sameDroidAgentID(guardID, auditID) {
|
||||
matched = true
|
||||
break
|
||||
if !sameDroidAgentID(guardID, auditID) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if matched {
|
||||
root := filepath.Join(runtimeRoot, guardID)
|
||||
contracts = append(contracts, droidRuntimeContract{
|
||||
guardID, filepath.Join(root, droidGuardScript), filepath.Join(root, droidAuditScript),
|
||||
guardID,
|
||||
filepath.Join(root, droidGuardScript),
|
||||
filepath.Join(root, droidAuditScript),
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
return contracts
|
||||
@@ -390,22 +419,14 @@ func managedDroidIDs(settings droidHookSettings, event, script, runtimeRoot stri
|
||||
ids := map[string]bool{}
|
||||
groups, _ := settings[event].([]any)
|
||||
for _, groupValue := range groups {
|
||||
for _, handlerValue := range groupValue.(map[string]any)["hooks"].([]any) {
|
||||
if agentID, ok := managedDroidAgentID(handlerValue.(map[string]any), script, runtimeRoot); ok {
|
||||
ids[agentID] = true
|
||||
}
|
||||
group := groupValue.(map[string]any)
|
||||
handlers := group["hooks"].([]any)
|
||||
if len(group) != 2 || group["matcher"] != "*" || len(handlers) != 1 {
|
||||
continue
|
||||
}
|
||||
if agentID, ok := managedDroidAgentID(handlers[0].(map[string]any), script, runtimeRoot); ok {
|
||||
ids[agentID] = true
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func mergeDroidSettings(primary, fallback droidHookSettings) droidHookSettings {
|
||||
merged := make(droidHookSettings, len(primary)+len(fallback))
|
||||
for key, value := range fallback {
|
||||
merged[key] = value
|
||||
}
|
||||
for key, value := range primary {
|
||||
merged[key] = value
|
||||
}
|
||||
return merged
|
||||
}
|
||||
Reference in new issue
Block a user