Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(daemon): use CREATE_NEW_CONSOLE to stop grandchild console popups on Windows (#1521) (#1643)
Bohan Jiang74593fdb8883
diff --git a/server/pkg/agent/proc_windows.go b/server/pkg/agent/proc_windows.goindex 9f13ca0c66..27e5681d81 100644--- a/server/pkg/agent/proc_windows.go+++ b/server/pkg/agent/proc_windows.go@@ -7,14 +7,26 @@ import ("syscall")-const createNoWindow = 0x08000000+// createNewConsole allocates a fresh console for the child process. Combined+// with HideWindow=true (STARTF_USESHOWWINDOW + SW_HIDE) the console window+// stays off-screen, and — critically — any grandchildren the agent spawns+// (tool subprocesses like bash, cmd, netstat, findstr) inherit this hidden+// console instead of each allocating their own visible one.+//+// Using CREATE_NO_WINDOW here instead would strip the console entirely,+// which forces Windows to allocate a new visible console per grandchild+// when the grandchild is a console-subsystem program that doesn't itself+// pass CREATE_NO_WINDOW — the exact popup storm reported in #1521.+const createNewConsole = 0x00000010-// hideAgentWindow configures cmd to suppress the console window on Windows.-// CREATE_NO_WINDOW prevents window creation while preserving stdio pipes.+// hideAgentWindow configures cmd to suppress the console window on Windows+// while still giving descendant processes a hidden console to inherit.+// Stdio pipes set via cmd.StdoutPipe/StdinPipe keep working because+// STARTF_USESTDHANDLES takes precedence over the new console's stdio.func hideAgentWindow(cmd *exec.Cmd) {if cmd.SysProcAttr == nil {cmd.SysProcAttr = &syscall.SysProcAttr{}}cmd.SysProcAttr.HideWindow = true- cmd.SysProcAttr.CreationFlags |= createNoWindow+ cmd.SysProcAttr.CreationFlags |= createNewConsole}diff --git a/server/pkg/agent/proc_windows_test.go b/server/pkg/agent/proc_windows_test.gonew file mode 100644index 0000000000..58a55b481c--- /dev/null+++ b/server/pkg/agent/proc_windows_test.go@@ -0,0 +1,65 @@+//go:build windows++package agent++import (+ "os/exec"+ "syscall"+ "testing"+)++// TestHideAgentWindowSetsCreateNewConsole guards against a regression where+// hideAgentWindow reverts to CREATE_NO_WINDOW. CREATE_NO_WINDOW strips the+// console entirely, which forces Windows to allocate a new visible console+// per grandchild that doesn't itself pass CREATE_NO_WINDOW — the popup+// storm reported in #1521.+func TestHideAgentWindowSetsCreateNewConsole(t *testing.T) {+ cmd := exec.Command("cmd.exe", "/c", "echo", "hi")+ hideAgentWindow(cmd)++ if cmd.SysProcAttr == nil {+ t.Fatal("SysProcAttr should be initialized")+ }+ if !cmd.SysProcAttr.HideWindow {+ t.Error("HideWindow should be true")+ }+ if cmd.SysProcAttr.CreationFlags&createNewConsole == 0 {+ t.Errorf("CreationFlags should include CREATE_NEW_CONSOLE (0x%x), got 0x%x",+ createNewConsole, cmd.SysProcAttr.CreationFlags)+ }+ const createNoWindow = 0x08000000+ if cmd.SysProcAttr.CreationFlags&createNoWindow != 0 {+ t.Errorf("CreationFlags must NOT include CREATE_NO_WINDOW (0x%x), got 0x%x — "++ "see #1521 for why this causes grandchild popups",+ createNoWindow, cmd.SysProcAttr.CreationFlags)+ }+}++// TestHideAgentWindowPreservesExistingSysProcAttr ensures hideAgentWindow+// does not overwrite fields set by callers — a regression caught in PR #1474+// where the whole SysProcAttr struct was replaced. We verify both a+// non-CreationFlags field and a pre-existing CreationFlags bit survive.+//+// CREATE_UNICODE_ENVIRONMENT (0x00000400) is chosen because it is documented+// as compatible with CREATE_NEW_CONSOLE (unlike CREATE_NEW_PROCESS_GROUP,+// which Windows silently ignores when combined with CREATE_NEW_CONSOLE), so+// a surviving bit here is semantically meaningful, not just bitwise intact.+func TestHideAgentWindowPreservesExistingSysProcAttr(t *testing.T) {+ const createUnicodeEnvironment = 0x00000400+ cmd := exec.Command("cmd.exe", "/c", "echo", "hi")+ cmd.SysProcAttr = &syscall.SysProcAttr{+ CreationFlags: createUnicodeEnvironment,+ NoInheritHandles: true,+ }+ hideAgentWindow(cmd)++ if !cmd.SysProcAttr.NoInheritHandles {+ t.Error("NoInheritHandles set by caller should be preserved")+ }+ if cmd.SysProcAttr.CreationFlags&createUnicodeEnvironment == 0 {+ t.Error("existing CreationFlags bits (CREATE_UNICODE_ENVIRONMENT) should be preserved")+ }+ if cmd.SysProcAttr.CreationFlags&createNewConsole == 0 {+ t.Error("CREATE_NEW_CONSOLE should be OR'd into existing flags")+ }+}