Repository navigation
Fix system.run hang, harden argv quoting, and add ShellQuoting tests - #38
Merged
Merged
Conversation
Co-authored-by: shanselman <[email protected]>
When system.run receives a command as an argv array, BuildProcessArgs joins the args with spaces before passing to the shell. Args containing spaces (e.g. a -p prompt like 'List all files') would be split into separate tokens by the shell, causing tools like GitHub Copilot CLI to report 'too many arguments'. Added QuoteArgIfNeeded() that wraps args containing spaces, quotes, or tabs in double quotes with proper escaping.
The gateway's nodes 'run' action requires system.run.prepare to build an execution plan before the actual run. Without it, the only path is the 'invoke' action which has a hardcoded 30s gateway timeout — too short for long-running tools like Copilot CLI. system.run.prepare echoes back the argv, cwd, rawCommand, agentId, and sessionKey without executing anything, enabling the gateway to use its configurable timeout path for the actual execution.
Address review findings from PRs #29 and #32: - QuoteArgIfNeeded: check all shell metacharacters (& | ; < > etc.), not just whitespace/quotes — prevents command injection - cmd.exe: use doubled-quote escaping (%"%") instead of backslash which cmd doesn't recognize - FormatExecCommand: align metacharacter checks with QuoteArgIfNeeded so system.run.prepare shows the same command that actually executes - StringBuilder: add lock around stdout/stderr event handlers and ToString() reads to prevent concurrent-access corruption Co-authored-by: danedane <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Copilot <[email protected]>
Extract duplicated metacharacter quoting logic from LocalCommandRunner and SystemCapability into a shared ShellQuoting static class, following the established GatewayUrlHelper pattern. - ShellQuoting.NeedsQuoting: single source of truth for metachar detection - ShellQuoting.QuoteForShell: shell-aware quoting (cmd vs PowerShell) - ShellQuoting.FormatExecCommand: display formatting for gateway - IsShellMetachar: idiomatic switch expression instead of duplicated multi-case switch blocks - 43 new unit tests covering all metacharacters, escaping edge cases, null handling, and both shell modes Co-authored-by: danedane <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Copilot <[email protected]>
This was referenced Mar 17, 2026
Closed
This was referenced Jun 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR consolidates and improves work from #29 and #32, fixing a regression where
system.runhangs indefinitely for IPC-connected CLI tools, hardening argv quoting against command injection, and extracting shared quoting logic with comprehensive test coverage.Fixes #28
What's included
From PR #29 — Fix
WaitForExitAsynchang (by @copilot-swe-agent)Originally reported by @milosm in #28 — thank you for the excellent bug report with clear repro steps and root cause analysis! 🙏
WaitForExitAsyncwithExitedevent +TaskCompletionSourceto avoid hanging when child processes inherit the stdout pipe write handleRun_CompletesPromptly_WhenOrphanChildProcessHoldsStdoutHandleFrom PR #32 — Shell-aware argv quoting +
system.run.prepare(by @danedane)Thank you @danedane for identifying the arg-splitting bug and adding the
system.run.preparepre-flight handler — great work! 🎉QuoteArgIfNeededwith shell-appropriate escaping (cmd.exe double quotes, PowerShell single quotes)system.run.preparehandler that echoes execution plans without executingFormatExecCommandhelper matching gateway behaviorNew in this PR — Review fixes and refactoring
Code review found several issues that are addressed here:
QuoteArgIfNeededonly checked spaces/quotes& | ; < > ^ % ! $ \* ?` etc.)\") which cmd doesn't recognize"")lockaround stdout/stderr buildersShellQuotinghelper (followsGatewayUrlHelperpattern)FormatExecCommandandQuoteArgIfNeededchecked different charsShellQuoting.NeedsQuotingTest coverage
ShellQuotingTests.cscovering:FormatExecCommanddisplay formattingChanged files
src/OpenClaw.Shared/ShellQuoting.cs— new shared quoting helpersrc/OpenClaw.Shared/LocalCommandRunner.cs— hang fix + usesShellQuotingsrc/OpenClaw.Shared/Capabilities/SystemCapability.cs—system.run.prepare+ delegates toShellQuotingtests/OpenClaw.Shared.Tests/ShellQuotingTests.cs— new 43 unit teststests/OpenClaw.Shared.Tests/SystemRunTests.cs— IPC hang regression test