fix(qwen): align Go hooks with Qwen Code 0.20
This commit is contained in:
1 parent
4521cbe32f
commit
dd72793cdb
18 files changed
+2921
-741
No files matched your search
@@ -1,8 +1,6 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
@@ -13,12 +11,25 @@ const (
|
||||
qwenAgentKey = "qwen"
|
||||
qwenGuardScript = "guard.js"
|
||||
qwenAuditScript = "hook.js"
|
||||
qwenGuardHookName = "elydora-guard"
|
||||
qwenAuditHookName = "elydora-audit"
|
||||
qwenHookTimeout = float64(10_000)
|
||||
qwenOwnedFileMarker = "// Managed by Elydora"
|
||||
qwenPOSIXApostrophe = `'"'"'`
|
||||
)
|
||||
|
||||
var qwenToolEvents = [...]string{"PreToolUse", "PostToolUse"}
|
||||
var qwenManagedEvents = [...]string{
|
||||
"PreToolUse",
|
||||
"PostToolUse",
|
||||
"PostToolUseFailure",
|
||||
}
|
||||
|
||||
var qwenEventContracts = [...]struct {
|
||||
event, script, name string
|
||||
}{
|
||||
{"PreToolUse", qwenGuardScript, qwenGuardHookName},
|
||||
{"PostToolUse", qwenAuditScript, qwenAuditHookName},
|
||||
{"PostToolUseFailure", qwenAuditScript, qwenAuditHookName},
|
||||
}
|
||||
|
||||
type qwenHookSettings map[string]any
|
||||
|
||||
@@ -35,214 +46,208 @@ type qwenRuntimeContract struct {
|
||||
auditPath string
|
||||
}
|
||||
|
||||
func buildQwenCommand(nodePath, scriptPath string) string {
|
||||
func qwenExpectedShell() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return "& " + quoteQwenPowerShell(nodePath) + " " + quoteQwenPowerShell(scriptPath) + "; exit $LASTEXITCODE"
|
||||
return "powershell"
|
||||
}
|
||||
return quotePOSIXArgument(nodePath) + " " + quotePOSIXArgument(scriptPath)
|
||||
return "bash"
|
||||
}
|
||||
|
||||
func quoteQwenPowerShell(value string) string {
|
||||
return "'" + strings.ReplaceAll(value, "'", "''") + "'"
|
||||
}
|
||||
|
||||
func buildQwenGroup(nodePath, scriptPath string) map[string]any {
|
||||
shell := "bash"
|
||||
if runtime.GOOS == "windows" {
|
||||
shell = "powershell"
|
||||
}
|
||||
func buildQwenGroup(nodePath, scriptPath, name string) map[string]any {
|
||||
return map[string]any{
|
||||
"matcher": "*",
|
||||
"hooks": []any{map[string]any{
|
||||
"type": "command", "command": buildQwenCommand(nodePath, scriptPath),
|
||||
"shell": shell, "timeout": qwenHookTimeout,
|
||||
"type": "command",
|
||||
"name": name,
|
||||
"command": buildQwenCommand(nodePath, scriptPath),
|
||||
"shell": qwenExpectedShell(),
|
||||
"timeout": qwenHookTimeout,
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func parseQwenCommand(command string) (string, string, bool) {
|
||||
start := 0
|
||||
if runtime.GOOS == "windows" {
|
||||
if !strings.HasPrefix(command, "& ") {
|
||||
return "", "", false
|
||||
}
|
||||
start = 2
|
||||
}
|
||||
executable, next, ok := readQwenQuotedArgument(command, start)
|
||||
if !ok || next >= len(command) || command[next] != ' ' {
|
||||
return "", "", false
|
||||
}
|
||||
script, end, ok := readQwenQuotedArgument(command, next+1)
|
||||
expectedSuffix := ""
|
||||
if runtime.GOOS == "windows" {
|
||||
expectedSuffix = "; exit $LASTEXITCODE"
|
||||
}
|
||||
return executable, script, ok && command[end:] == expectedSuffix && executable != "" && script != ""
|
||||
}
|
||||
|
||||
func readQwenQuotedArgument(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); {
|
||||
if runtime.GOOS == "windows" && strings.HasPrefix(command[index:], "''") {
|
||||
value.WriteByte('\'')
|
||||
index += 2
|
||||
continue
|
||||
}
|
||||
if runtime.GOOS != "windows" && strings.HasPrefix(command[index:], qwenPOSIXApostrophe) {
|
||||
value.WriteByte('\'')
|
||||
index += len(qwenPOSIXApostrophe)
|
||||
continue
|
||||
}
|
||||
if command[index] == '\'' {
|
||||
return value.String(), index + 1, true
|
||||
}
|
||||
value.WriteByte(command[index])
|
||||
index++
|
||||
}
|
||||
return "", start, false
|
||||
}
|
||||
|
||||
func sameQwenPath(left, right string) bool {
|
||||
left = filepath.Clean(left)
|
||||
right = filepath.Clean(right)
|
||||
if absolute, err := filepath.Abs(left); err == nil {
|
||||
left = absolute
|
||||
}
|
||||
if absolute, err := filepath.Abs(right); err == nil {
|
||||
right = absolute
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.EqualFold(left, right)
|
||||
}
|
||||
return left == right
|
||||
}
|
||||
|
||||
func sameQwenAgentID(left, right string) bool {
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.EqualFold(left, right)
|
||||
}
|
||||
return left == right
|
||||
}
|
||||
|
||||
func qwenRuntimeRoot() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve home directory: %w", err)
|
||||
}
|
||||
return filepath.Join(home, ".elydora"), nil
|
||||
}
|
||||
|
||||
func managedQwenAgentID(handler map[string]any, scriptName, runtimeRoot string) (string, bool) {
|
||||
if len(handler) != 4 || handler["type"] != "command" || handler["timeout"] != qwenHookTimeout {
|
||||
return "", false
|
||||
}
|
||||
expectedShell := "bash"
|
||||
if runtime.GOOS == "windows" {
|
||||
expectedShell = "powershell"
|
||||
}
|
||||
if handler["shell"] != expectedShell {
|
||||
return "", false
|
||||
func currentQwenReference(
|
||||
handler map[string]any,
|
||||
scriptName, hookName, runtimeRoot string,
|
||||
) (qwenRuntimeReference, bool) {
|
||||
if len(handler) != 5 || handler["type"] != "command" ||
|
||||
handler["name"] != hookName || handler["shell"] != qwenExpectedShell() ||
|
||||
handler["timeout"] != qwenHookTimeout {
|
||||
return qwenRuntimeReference{}, false
|
||||
}
|
||||
command, ok := handler["command"].(string)
|
||||
if !ok {
|
||||
return "", false
|
||||
return qwenRuntimeReference{}, false
|
||||
}
|
||||
executable, scriptPath, ok := parseQwenCommand(command)
|
||||
if !ok || !filepath.IsAbs(executable) || !filepath.IsAbs(scriptPath) ||
|
||||
!isQwenNodeExecutable(executable) || !sameQwenFileName(filepath.Base(scriptPath), scriptName) {
|
||||
return "", false
|
||||
}
|
||||
agentDirectory := filepath.Dir(scriptPath)
|
||||
if !sameQwenPath(filepath.Dir(agentDirectory), runtimeRoot) {
|
||||
return "", false
|
||||
}
|
||||
agentID := filepath.Base(agentDirectory)
|
||||
return agentID, agentID != "" && agentID != "." && agentID != ".."
|
||||
return qwenRuntimeReferenceForCommand(command, scriptName, runtimeRoot)
|
||||
}
|
||||
|
||||
func isQwenNodeExecutable(path string) bool {
|
||||
name := filepath.Base(path)
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.EqualFold(name, "node.exe")
|
||||
func legacyQwenReference(
|
||||
handler map[string]any,
|
||||
scriptName, runtimeRoot string,
|
||||
) (qwenRuntimeReference, bool) {
|
||||
if len(handler) != 4 || handler["type"] != "command" ||
|
||||
handler["shell"] != qwenExpectedShell() ||
|
||||
handler["timeout"] != qwenHookTimeout {
|
||||
return qwenRuntimeReference{}, false
|
||||
}
|
||||
return name == "node"
|
||||
command, ok := handler["command"].(string)
|
||||
if !ok {
|
||||
return qwenRuntimeReference{}, false
|
||||
}
|
||||
return qwenRuntimeReferenceForCommand(command, scriptName, runtimeRoot)
|
||||
}
|
||||
|
||||
func sameQwenFileName(left, right string) bool {
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.EqualFold(left, right)
|
||||
func managedQwenReference(
|
||||
handler map[string]any,
|
||||
scriptName, hookName, runtimeRoot string,
|
||||
includeLegacy bool,
|
||||
) (qwenRuntimeReference, bool) {
|
||||
if reference, ok := currentQwenReference(
|
||||
handler,
|
||||
scriptName,
|
||||
hookName,
|
||||
runtimeRoot,
|
||||
); ok {
|
||||
return reference, true
|
||||
}
|
||||
return left == right
|
||||
if includeLegacy {
|
||||
return legacyQwenReference(handler, scriptName, runtimeRoot)
|
||||
}
|
||||
return qwenRuntimeReference{}, false
|
||||
}
|
||||
|
||||
func managedQwenRemovals(settings qwenHookSettings, agentID, runtimeRoot string) []qwenManagedRemoval {
|
||||
func exactCurrentQwenGroup(group map[string]any) bool {
|
||||
_, hasHooks := group["hooks"]
|
||||
return len(group) == 1 && hasHooks
|
||||
}
|
||||
|
||||
func exactLegacyQwenGroup(group map[string]any) bool {
|
||||
_, hasHooks := group["hooks"]
|
||||
return len(group) == 2 && hasHooks && group["matcher"] == "*"
|
||||
}
|
||||
|
||||
func managedQwenRemovals(
|
||||
settings qwenHookSettings,
|
||||
agentID, runtimeRoot string,
|
||||
) []qwenManagedRemoval {
|
||||
removals := make([]qwenManagedRemoval, 0)
|
||||
for _, contract := range []struct{ event, script string }{
|
||||
{"PreToolUse", qwenGuardScript}, {"PostToolUse", qwenAuditScript},
|
||||
} {
|
||||
for _, contract := range qwenEventContracts {
|
||||
groups, _ := settings[contract.event].([]any)
|
||||
for groupIndex, groupValue := range groups {
|
||||
group := groupValue.(map[string]any)
|
||||
handlers := group["hooks"].([]any)
|
||||
indexes := make([]int, 0)
|
||||
for handlerIndex, handlerValue := range handlers {
|
||||
managedID, managed := managedQwenAgentID(handlerValue.(map[string]any), contract.script, runtimeRoot)
|
||||
if managed && (agentID == "" || sameQwenAgentID(managedID, agentID)) {
|
||||
reference, managed := managedQwenReference(
|
||||
handlerValue.(map[string]any),
|
||||
contract.script,
|
||||
contract.name,
|
||||
runtimeRoot,
|
||||
true,
|
||||
)
|
||||
if managed && (agentID == "" || sameQwenAgentID(reference.agentID, agentID)) {
|
||||
indexes = append(indexes, handlerIndex)
|
||||
}
|
||||
}
|
||||
if len(indexes) == 0 {
|
||||
continue
|
||||
}
|
||||
_, hasMatcher := group["matcher"]
|
||||
_, hasHooks := group["hooks"]
|
||||
exactGroup := len(group) == 2 && hasMatcher && hasHooks && group["matcher"] == "*" && len(indexes) == len(handlers)
|
||||
removals = append(removals, qwenManagedRemoval{contract.event, groupIndex, indexes, exactGroup})
|
||||
removeGroup := (exactCurrentQwenGroup(group) || exactLegacyQwenGroup(group)) &&
|
||||
len(indexes) == len(handlers)
|
||||
removals = append(removals, qwenManagedRemoval{
|
||||
event: contract.event, groupIndex: groupIndex,
|
||||
handlerIndexes: indexes, removeGroup: removeGroup,
|
||||
})
|
||||
}
|
||||
}
|
||||
return removals
|
||||
}
|
||||
|
||||
func qwenRuntimeContracts(settings qwenHookSettings, runtimeRoot string) []qwenRuntimeContract {
|
||||
guards := managedQwenIDs(settings, "PreToolUse", qwenGuardScript, runtimeRoot)
|
||||
audits := managedQwenIDs(settings, "PostToolUse", qwenAuditScript, runtimeRoot)
|
||||
ids := make([]string, 0, len(guards))
|
||||
for guardID := range guards {
|
||||
for auditID := range audits {
|
||||
if sameQwenAgentID(guardID, auditID) {
|
||||
ids = append(ids, guardID)
|
||||
break
|
||||
func normalizedQwenAgentID(agentID string) string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.ToLower(agentID)
|
||||
}
|
||||
return agentID
|
||||
}
|
||||
|
||||
func qwenReferencesForEvent(
|
||||
settings qwenHookSettings,
|
||||
event, scriptName, hookName, runtimeRoot string,
|
||||
) map[string][]qwenRuntimeReference {
|
||||
references := map[string][]qwenRuntimeReference{}
|
||||
groups, _ := settings[event].([]any)
|
||||
for _, groupValue := range groups {
|
||||
group := groupValue.(map[string]any)
|
||||
if !exactCurrentQwenGroup(group) {
|
||||
continue
|
||||
}
|
||||
for _, handlerValue := range group["hooks"].([]any) {
|
||||
reference, ok := currentQwenReference(
|
||||
handlerValue.(map[string]any),
|
||||
scriptName,
|
||||
hookName,
|
||||
runtimeRoot,
|
||||
)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key := normalizedQwenAgentID(reference.agentID)
|
||||
references[key] = append(references[key], reference)
|
||||
}
|
||||
}
|
||||
sort.Slice(ids, func(left, right int) bool {
|
||||
if runtime.GOOS == "windows" {
|
||||
return strings.ToLower(ids[left]) < strings.ToLower(ids[right])
|
||||
return references
|
||||
}
|
||||
|
||||
func qwenRuntimeContracts(
|
||||
settings qwenHookSettings,
|
||||
runtimeRoot string,
|
||||
) []qwenRuntimeContract {
|
||||
guards := qwenReferencesForEvent(
|
||||
settings,
|
||||
"PreToolUse",
|
||||
qwenGuardScript,
|
||||
qwenGuardHookName,
|
||||
runtimeRoot,
|
||||
)
|
||||
posts := qwenReferencesForEvent(
|
||||
settings,
|
||||
"PostToolUse",
|
||||
qwenAuditScript,
|
||||
qwenAuditHookName,
|
||||
runtimeRoot,
|
||||
)
|
||||
failures := qwenReferencesForEvent(
|
||||
settings,
|
||||
"PostToolUseFailure",
|
||||
qwenAuditScript,
|
||||
qwenAuditHookName,
|
||||
runtimeRoot,
|
||||
)
|
||||
contracts := make([]qwenRuntimeContract, 0)
|
||||
for key, guard := range guards {
|
||||
post := posts[key]
|
||||
failure := failures[key]
|
||||
if len(guard) != 1 || len(post) != 1 || len(failure) != 1 ||
|
||||
!sameQwenPath(post[0].scriptPath, failure[0].scriptPath) {
|
||||
continue
|
||||
}
|
||||
return ids[left] < ids[right]
|
||||
})
|
||||
contracts := make([]qwenRuntimeContract, 0, len(ids))
|
||||
for _, agentID := range ids {
|
||||
root := filepath.Join(runtimeRoot, agentID)
|
||||
contracts = append(contracts, qwenRuntimeContract{
|
||||
agentID, filepath.Join(root, qwenGuardScript), filepath.Join(root, qwenAuditScript),
|
||||
agentID: guard[0].agentID,
|
||||
guardPath: guard[0].scriptPath,
|
||||
auditPath: post[0].scriptPath,
|
||||
})
|
||||
}
|
||||
sort.Slice(contracts, func(left, right int) bool {
|
||||
return normalizedQwenAgentID(contracts[left].agentID) <
|
||||
normalizedQwenAgentID(contracts[right].agentID)
|
||||
})
|
||||
return contracts
|
||||
}
|
||||
|
||||
func managedQwenIDs(settings qwenHookSettings, event, script, runtimeRoot string) map[string]bool {
|
||||
ids := map[string]bool{}
|
||||
groups, _ := settings[event].([]any)
|
||||
for _, groupValue := range groups {
|
||||
for _, handlerValue := range groupValue.(map[string]any)["hooks"].([]any) {
|
||||
if agentID, ok := managedQwenAgentID(handlerValue.(map[string]any), script, runtimeRoot); ok {
|
||||
ids[agentID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return ids
|
||||
func qwenRuntimeRoot() (string, error) {
|
||||
return AgentRuntimeRoot()
|
||||
}
|
||||
|
||||
func qwenContractDirectory(contract qwenRuntimeContract) string {
|
||||
return filepath.Dir(contract.guardPath)
|
||||
}
|
||||
Reference in new issue
Block a user