-
Notifications
You must be signed in to change notification settings - Fork 52
fix: replace hand-rolled cmdline escaping with JSON-encoded --agent-config #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yeelam-gordon
wants to merge
23
commits into
main
Choose a base branch
from
dev/yeelam/cmdline-builder-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
42fee12
Extract cmdline builder with proper quoting and add unit tests
yeelam-gordon a5be1e8
feat: pass agent config as JSON-encoded --agent-config argument
yeelam-gordon 221c122
fix: correct misleading comments about JsonCpp usage
yeelam-gordon ff5e4e2
chore: trigger re-review
yeelam-gordon ef17c67
fix: address Copilot review round 2
yeelam-gordon ad520e5
chore: request re-review
yeelam-gordon 92e663d
fix: make QuoteArgForCommandLine non-throwing (address round 3)
yeelam-gordon e519a19
chore: trigger review
yeelam-gordon 9213afe
fix: address round 4 Copilot review comments
yeelam-gordon dfdae64
chore: trigger review
yeelam-gordon b44b2e3
fix: quote wta path with QuoteProgramPath in all launch paths
yeelam-gordon 7b7bcc7
chore: trigger review
yeelam-gordon 183e02d
fix: make BuildAgentConfigArg return std::optional
yeelam-gordon 59b62da
chore: trigger review
yeelam-gordon 9cac907
fix: address round 7 - logging and cross-platform guard
yeelam-gordon aefece2
chore: trigger review
yeelam-gordon a7460ee
chore: trigger copilot review
yeelam-gordon e204334
chore: trigger review after resolving threads
yeelam-gordon 0f8228d
chore: trigger copilot review
yeelam-gordon 6d3eaf9
fix: use ../inc/ prefix for QuoteArgForCommandLine.h include
yeelam-gordon 3b5d2dd
fix: address round 8 - noexcept safety and overlay dedup
yeelam-gordon b1dd16c
fix: resolve check-spelling alerts in cmdline tests
yeelam-gordon 6cf87a5
fix: destructure AgentConfig in overlay blocks
yeelam-gordon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| #include "../inc/AgentRegistry.h" | ||
| #include "../inc/AgentHooksStatus.h" | ||
| #include "../inc/WtaProcess.h" | ||
| #include "../inc/QuoteArgForCommandLine.h" | ||
|
|
||
| #include <json/json.h> | ||
|
|
||
|
|
@@ -966,13 +967,9 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation | |
| std::string stdoutText; | ||
| if (!wtaPath.empty()) | ||
| { | ||
| // Quote-escape internal `"` per Windows CRT rules. | ||
| std::wstring escaped = agentCmdline; | ||
| for (size_t pos = 0; (pos = escaped.find(L'"', pos)) != std::wstring::npos; pos += 2) | ||
| { | ||
| escaped.replace(pos, 1, L"\"\""); | ||
| } | ||
| const std::wstring args = L"probe-models --agent \"" + escaped + L"\""; | ||
| // Use correct CommandLineToArgvW quoting for the agent argument. | ||
| const std::wstring args = L"probe-models --agent " + | ||
| ::Microsoft::Terminal::CommandLine::QuoteArgForCommandLine(std::wstring_view{ agentCmdline }); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 92e663d — QuoteArgForCommandLine returns std::optional. Probe path skips launch if quoting fails. |
||
| // 40s ceiling matches probe.rs's internal limits (npx | ||
| // initialize 25s + new_session 10s + slack). Cached | ||
| // adapters return in <2s. | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| // QuoteArgForCommandLine.h | ||
| // | ||
| // Correct CommandLineToArgvW-compatible quoting for a single argument. | ||
| // Eliminates hand-rolled escaping throughout the codebase. Use this | ||
| // whenever building a commandline string for CreateProcess/ShellExecute. | ||
| // | ||
| // Pure Win32 + STL, no WinRT dependency. | ||
|
|
||
| #pragma once | ||
|
|
||
|
yeelam-gordon marked this conversation as resolved.
|
||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace Microsoft::Terminal::CommandLine | ||
| { | ||
| // Quote a single argument for use in a Windows commandline string. | ||
| // The result is always wrapped in double quotes for unambiguous parsing | ||
| // by CommandLineToArgvW. Handles: | ||
| // - Backslashes before `"` are doubled (2n+1 backslashes + `"`) | ||
| // - Trailing backslashes before the closing `"` are doubled | ||
| // - All other characters are passed through literally | ||
| // | ||
| // NOTE: This is for argv[1..n] only. argv[0] (the program path) has | ||
| // different rules — backslashes are always literal, and `"` cannot be | ||
| // escaped inside it. For argv[0], simply wrap in quotes if needed and | ||
| // reject paths containing `"`. | ||
| inline std::wstring QuoteArgForCommandLine(std::wstring_view arg) | ||
| { | ||
| std::wstring result; | ||
| // Reserve: arg size + quotes + some escaping headroom | ||
| result.reserve(arg.size() + 8); | ||
| result.push_back(L'"'); | ||
|
|
||
| size_t backslashes = 0; | ||
| for (const auto ch : arg) | ||
|
yeelam-gordon marked this conversation as resolved.
Outdated
|
||
| { | ||
| if (ch == L'\\') | ||
| { | ||
| ++backslashes; | ||
| } | ||
| else if (ch == L'"') | ||
| { | ||
| // Double the accumulated backslashes, then emit \" | ||
| result.append(backslashes * 2 + 1, L'\\'); | ||
| result.push_back(L'"'); | ||
| backslashes = 0; | ||
| } | ||
| else | ||
| { | ||
| // Flush any accumulated backslashes as-is | ||
| result.append(backslashes, L'\\'); | ||
| backslashes = 0; | ||
| result.push_back(ch); | ||
| } | ||
| } | ||
| // Trailing backslashes must be doubled (they precede the closing `"`) | ||
| result.append(backslashes * 2, L'\\'); | ||
| result.push_back(L'"'); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // Build a JSON-encoded `--agent-config` argument from the given fields. | ||
| // Returns the full fragment: ` --agent-config "<escaped-json>"` | ||
| // Uses manual RFC 8259-compliant JSON construction (no external JSON | ||
| // library dependency) and QuoteArgForCommandLine for the single | ||
| // argument boundary. | ||
| // | ||
| // Usage: | ||
| // cmdline += BuildAgentConfigArg(agentCli, agentId, delegateAgent, | ||
| // delegateModel, acpModel); | ||
| // | ||
| // Any empty field is omitted from the JSON (the Rust side uses | ||
| // Option<String> and falls back to defaults for missing fields). | ||
| inline std::wstring BuildAgentConfigArg( | ||
| std::wstring_view agent, | ||
| std::wstring_view agentId, | ||
| std::wstring_view delegateAgent, | ||
| std::wstring_view delegateModel, | ||
| std::wstring_view acpModel) | ||
| { | ||
| // Build a compact JSON object with only non-empty fields. | ||
| // We use manual JSON construction to avoid pulling in JsonCpp here | ||
| // (this header is used in both TerminalApp and TerminalSettingsEditor). | ||
| // The JSON spec is simple enough for known-safe field names: only the | ||
| // VALUES need escaping, and we do it correctly per RFC 8259. | ||
| auto jsonEscapeValue = [](std::wstring_view val) -> std::wstring { | ||
| std::wstring out; | ||
| out.reserve(val.size() + 4); | ||
| for (const auto ch : val) | ||
| { | ||
| switch (ch) | ||
| { | ||
| case L'"': out += L"\\\""; break; | ||
| case L'\\': out += L"\\\\"; break; | ||
| case L'\b': out += L"\\b"; break; | ||
| case L'\f': out += L"\\f"; break; | ||
| case L'\n': out += L"\\n"; break; | ||
| case L'\r': out += L"\\r"; break; | ||
| case L'\t': out += L"\\t"; break; | ||
| default: | ||
| if (ch < 0x20) | ||
| { | ||
| wchar_t buf[8]; | ||
| swprintf_s(buf, L"\\u%04x", static_cast<unsigned>(ch)); | ||
| out += buf; | ||
| } | ||
| else | ||
| { | ||
| out.push_back(ch); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| return out; | ||
| }; | ||
|
|
||
| std::wstring json = L"{"; | ||
| bool first = true; | ||
|
|
||
| auto appendField = [&](const wchar_t* key, std::wstring_view val) { | ||
| if (val.empty()) | ||
| return; | ||
| if (!first) | ||
| json += L','; | ||
| first = false; | ||
| json += L'"'; | ||
| json += key; | ||
| json += L"\":\""; | ||
| json += jsonEscapeValue(val); | ||
| json += L'"'; | ||
| }; | ||
|
|
||
| appendField(L"agent", agent); | ||
| appendField(L"agentId", agentId); | ||
| appendField(L"delegateAgent", delegateAgent); | ||
| appendField(L"delegateModel", delegateModel); | ||
| appendField(L"acpModel", acpModel); | ||
|
|
||
| json += L'}'; | ||
|
|
||
| return L" --agent-config " + QuoteArgForCommandLine(json); | ||
| } | ||
|
yeelam-gordon marked this conversation as resolved.
|
||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.