Implementation:1ay1 Agentty PowerShell Command Encoder
| Knowledge Sources | |
|---|---|
| Domains | Encoding, Platform |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Pure, platform-independent PowerShell command encoder that produces the `-EncodedCommand` base64 payload (base64 of UTF-16LE), with compile-time known-answer tests on every platform.
Description
The agentty::win_shell module is the pure PowerShell command encoder used by the Ctrl+G code-block runner's Windows path. A PowerShell block is run via `powershell -EncodedCommand <base64>`, where the base64 is of the UTF-16LE bytes of the script. This sidesteps every layer of cmd.exe/argv quoting (nested quotes, newlines, `$vars`, `&|<>` all pass through untouched) because the whole script is one opaque base64 argument.
This lives in a header — not buried `#if _WIN32` in codeblock.cpp — so the encoder can be unit-tested on ANY platform. `constexpr` functions with compile-time known-answer tests (`static_assert`) run at build time on EVERY platform, since the Windows runner path can't be compiled on the Linux/macOS CI hosts.
Usage
Import this when you need to: encode a PowerShell script for the `-EncodedCommand` flag, or produce the full `powershell` command line for the code-block runner.
Code Reference
Source Location
- Repository: 1ay1_Agentty
- File: include/agentty/runtime/win_shell_encode.hpp
- Lines: 1-94
Signature
namespace agentty::win_shell {
// Base64-encode (standard alphabet, '=' padded). Pure; constexpr.
[[nodiscard]] constexpr std::string base64(const std::string& bytes);
// Widen to UTF-16LE bytes (low byte then high byte per code unit).
[[nodiscard]] constexpr std::string to_utf16le_bytes(const std::string& body);
// The -EncodedCommand payload (base64 of UTF-16LE), no prefix.
[[nodiscard]] constexpr std::string encoded_command(const std::string& body);
// The complete command line the Windows runner spawns.
[[nodiscard]] constexpr std::string powershell_command(const std::string& body);
}
Import
#include "agentty/runtime/win_shell_encode.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| body | std::string | Yes | The PowerShell script body |
Outputs
| Name | Type | Description |
|---|---|---|
| encoded_command() | std::string | Base64 of UTF-16LE bytes |
| powershell_command() | std::string | "powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand <base64>" |
Usage Examples
Encode a PowerShell Block
#include "agentty/runtime/win_shell_encode.hpp"
auto script = R"(Write-Host "Hello, World!")";
auto cmd = agentty::win_shell::powershell_command(script);
// → "powershell -NoProfile -ExecutionPolicy Bypass -EncodedCommand VwByAGkAdABlAC0A..."
// The whole script is one opaque base64 arg — no quoting issues
Compile-Time Verification
// These static_asserts run at build time on EVERY platform:
static_assert(agentty::win_shell::encoded_command("echo hi") == "ZQBjAGgAbwAgAGgAaQA=");
static_assert(agentty::win_shell::encoded_command("a\nb") == "YQAKAGIA");
// If the encoding ever regresses, the build FAILS on Linux/macOS/Windows alike