From 40105c822831a858fcd717136ed6d09c6302a2d3 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Thu, 21 May 2026 06:56:09 +0000 Subject: [PATCH] fix: match daemon process flags exactly --- cmd/threadmark/process_args.go | 16 +++++++++++++++- cmd/threadmark/process_args_test.go | 25 +++++++++++++++++++++++++ cmd/threadmark/process_darwin.go | 5 ----- 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 cmd/threadmark/process_args_test.go diff --git a/cmd/threadmark/process_args.go b/cmd/threadmark/process_args.go index ce5dcca..cf49a2c 100644 --- a/cmd/threadmark/process_args.go +++ b/cmd/threadmark/process_args.go @@ -1,6 +1,9 @@ package main -import "strings" +import ( + "regexp" + "strings" +) func argvHasFlagValue(args []string, name, value string) bool { for idx := 0; idx < len(args)-1; idx++ { @@ -16,3 +19,14 @@ func argvHasFlagValue(args []string, name, value string) bool { } return false } + +func commandLineHasFlagValue(commandLine, name, value string) bool { + boundary := `(?:^|\s)` + end := `(?:\s|$)` + separate := regexp.MustCompile(boundary + regexp.QuoteMeta(name) + `\s+` + regexp.QuoteMeta(value) + end) + if separate.MatchString(commandLine) { + return true + } + equals := regexp.MustCompile(boundary + regexp.QuoteMeta(name+"="+value) + end) + return equals.MatchString(commandLine) +} diff --git a/cmd/threadmark/process_args_test.go b/cmd/threadmark/process_args_test.go new file mode 100644 index 0000000..f703c78 --- /dev/null +++ b/cmd/threadmark/process_args_test.go @@ -0,0 +1,25 @@ +package main + +import "testing" + +func TestCommandLineHasFlagValueRequiresExactTokenBoundary(t *testing.T) { + commandLine := "threadmarkd -root /tmp/threadmark-other -socket=/tmp/threadmark/daemon.sock-extra" + + if commandLineHasFlagValue(commandLine, "-root", "/tmp/threadmark") { + t.Fatal("matched root value prefix from longer flag value") + } + if commandLineHasFlagValue(commandLine, "-socket", "/tmp/threadmark/daemon.sock") { + t.Fatal("matched socket value prefix from longer flag value") + } +} + +func TestCommandLineHasFlagValueMatchesSeparateAndEqualsForms(t *testing.T) { + commandLine := "threadmarkd -root /tmp/threadmark -socket=/tmp/threadmark/daemon.sock" + + if !commandLineHasFlagValue(commandLine, "-root", "/tmp/threadmark") { + t.Fatal("did not match separate flag value") + } + if !commandLineHasFlagValue(commandLine, "-socket", "/tmp/threadmark/daemon.sock") { + t.Fatal("did not match equals flag value") + } +} diff --git a/cmd/threadmark/process_darwin.go b/cmd/threadmark/process_darwin.go index 939cef6..93de24c 100644 --- a/cmd/threadmark/process_darwin.go +++ b/cmd/threadmark/process_darwin.go @@ -34,8 +34,3 @@ func verifyDaemonProcess(pid int, root, socketPath string) error { } return nil } - -func commandLineHasFlagValue(commandLine, name, value string) bool { - return strings.Contains(commandLine, name+" "+value) || - strings.Contains(commandLine, name+"="+value) -}