cupcake
CLAUDE.md294 starsChanged 12 months agoApache-2.0View on GitHub
This document explains the Windows-specific requirements and fixes needed for the CI pipeline. Problem: OPA v1.7.1 on Windows has known bugs with path handling: Reference: https://github.com/open-policy-agent/opa/issues/4174 Solution Implemented (in cupcake-core/src/engine/compiler.rs): Problem: Tests that execute .sh files fail on Windows because: Affected Tests (all marked with #[cfg(not(windows))]): Why Tests Are Skipped on Windows: Solution Implemented (in cupcake-core/src/engine/mod.rs):
In this repositoryOne other file it gives its agents.
- CLAUDE.mdCLAUDE.md
The file
Show its rules and text
We split this CLAUDE.md into the rules it gives an agent
32 rules. About 0 are common, 17 are distinctive, and 15 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈17
Distinctive · estimate
This author's own choices. Worth a look.
15
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 39
Do NOT use canonicalize() on Windows (produces UNC paths)
- Distinctive: line 72
All policy evaluation and routing tests
- Distinctive: line 73
All trust mode and validation tests
- Distinctive: line 148
Test helpers ensure script content uses bash-compatible paths
- Very specific: line 154
Problem: All 10 Claude CLI routing tests failed on Windows due to invalid JSON in .claude/settings.json.
- Distinctive: line 164
Test code generated JSON with unescaped backslashes:
- Very specific: line 234
Always use serde_json for JSON generation in production code
- Distinctive: line 235
Test infrastructure can use manual escaping for simple cases
- Very specific: line 246
Must use \\ in JSON: "C:\\Users\\path"
- Very specific: line 247
Use command.replace('\\', "\\\\") before inserting into JSON
- Distinctive: line 252
Always convert to forward slashes before passing to OPA
- Very specific: line 253
Use path.replace('\\', "/") for OPA paths
- Very specific: line 254
Use Path::join() for filesystem operations (handles platform differences)
- Very specific: line 260
Key Rule: Never use string concatenation for paths - always use Path::join()
- Very specific: line 297
Use extensive eprintln!() debugging (tracing doesn't work in tests)
- Distinctive: line 298
Verify both the happy path AND error messages contain correct paths
- Very specific: line 303
Check actual paths: Use eprintln!("[DEBUG] Path: {:?}", path) liberally
- Very specific: line 304
Verify OPA version: Ensure v1.7.1+ is installed (has opa version in CI)
- Distinctive: line 316
Analyze the Error: Don't just read the error message - understand what the system is trying to do
- Distinctive: line 317
Trace Execution Flow: Follow code from discovery → storage → execution to find where platform differences matter
- Very specific: line 349
Fix Location: Test code (claude_code_routing_test.rs)
- Distinctive: line 356
Execution: Must use Unix format for Git Bash to interpret correctly
- Very specific: line 358
Fix Location: Test utility functions (path_for_bash())
- Distinctive: line 426
Pattern: Document not just the fix, but the _why_ behind it.
- Distinctive: line 461
Read stderr/stdout from failed tests
- Very specific: line 488
Test code inserted unescaped paths: "C:\Users\path" (invalid JSON)
- Distinctive: line 516
Check for platform-specific character escaping issues
- Distinctive: line 517
Test with minimal reproducible config outside the main application
- Distinctive: line 518
Add explicit validation/parsing in test setup if possible
- Distinctive: line 522
Best Practice: Create platform-aware helper functions for test setup:
- Distinctive: line 553
No shebang support - must detect script type by extension
- Very specific: line 579
Consider using a Rust tar library: instead of OPA's -o flag to avoid path issues entirely
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
GitHub Actions CI - Windows-Specific Notes
This document explains the Windows-specific requirements and fixes needed for the CI pipeline.
Critical Windows Issues and Solutions
1. OPA (Open Policy Agent) Path Handling Bug
Problem: OPA v1.7.1 on Windows has known bugs with path handling:
- Strips drive letters from normal paths:
C:\pathbecomes\path - Cannot write to
file://URL paths (syntax error) - UNC paths (
\\?\C:\) are also not supported
Reference: https://github.com/open-policy-agent/opa/issues/4174
Solution Implemented (in cupcake-core/src/engine/compiler.rs):
// INPUT: Use file:// URL format (OPA can read from URLs)
let temp_path_arg = if cfg!(windows) {
let url_path = temp_path_str.replace('\\', "/");
format!("file:///{}", url_path)
} else {
temp_path_str.to_string()
};
// OUTPUT: Use relative path with working directory set
let bundle_path_arg = if cfg!(windows) {
opa_cmd.current_dir(temp_path);
"bundle.tar.gz".to_string()
} else {
bundle_path_str.to_string()
};Key Points:
- Do NOT use
canonicalize()on Windows (produces UNC paths) - Input directory:
file:///C:/Users/pathformat - Output file: relative path with
.current_dir()set - This hybrid approach works around all OPA Windows path bugs
2. Shell Script Tests on Windows
Problem: Tests that execute .sh files fail on Windows because:
- Windows doesn't have native shell script support
- Requires Git Bash, WSL, or similar Unix environment
- GitHub Actions Windows runners have Git Bash but tests may not find it
Affected Tests (all marked with #[cfg(not(windows))]):
cupcake-core/tests/action_async_test.rstest_action_fire_and_forget- SKIPPED (timing-sensitive)test_multiple_actions_concurrent- SKIPPED (timing-sensitive)
cupcake-core/tests/action_discovery_test.rstest_action_discovery_from_directory- SKIPPED (timing-sensitive)test_discovery_with_rulebook_precedence- SKIPPED (timing-sensitive)test_action_discovery_ignores_subdirs- SKIPPED (timing-sensitive)
cupcake-core/tests/action_edge_cases_test.rstest_action_execution_edge_cases- SKIPPED (Unix-specific paths like /bin/echo)test_nonexistent_script_fallback- SKIPPED (Unix shell fallback behavior)
Why Tests Are Skipped on Windows:
- Timing-Sensitive Tests: Process spawning through Git Bash is slower than native Unix shells, making hard-coded timeouts unreliable
- Unix-Specific Behavior: Some tests use Unix-only paths (e.g.,
/bin/echo) or test Unix shell fallback semantics - Coverage: Core functionality is validated by tests that DO run on Windows:
test_action_failure_non_blocking- verifies actions don't block on failuretest_actions_dont_block_subsequent_evaluations- verifies async execution- All policy evaluation and routing tests
- All trust mode and validation tests
Solution Implemented (in cupcake-core/src/engine/mod.rs):
- Git Bash Detection - Automatically finds bash.exe at standard Windows install paths:
fn find_shell_command() -> &'static str {
if cfg!(windows) {
if std::path::Path::new(r"C:\Program Files\Git\bin\bash.exe").exists() {
return r"C:\Program Files\Git\bin\bash.exe";
}
if std::path::Path::new(r"C:\Program Files (x86)\Git\bin\bash.exe").exists() {
return r"C:\Program Files (x86)\Git\bin\bash.exe";
}
"bash.exe" // Fallback to PATH
} else {
"sh"
}
}- Windows Path Conversion for Git Bash - Converts Windows paths to Unix-style for .sh scripts:
// When executing .sh files on Windows, convert paths for Git Bash
if is_shell_script && cfg!(windows) {
// Convert C:\Users\foo\script.sh → /c/Users/foo/script.sh
let bash_path = if command.len() >= 3 && command.chars().nth(1) == Some(':') {
let drive = command.chars().next().unwrap().to_lowercase();
let path_part = &command[2..].replace('\\', "/");
format!("/{}{}", drive, path_part)
} else {
command.replace('\\', "/")
};
tokio::process::Command::new(*SHELL_COMMAND)
.arg(&bash_path) // Pass Unix-style path to bash
.output()
.await
}- Test Helper for Bash-Compatible Paths - Tests use
path_for_bash()to generate correct paths:
#[cfg(windows)]
fn path_for_bash(path: &PathBuf) -> String {
// Converts C:\path\to\file.txt → /c/path/to/file.txt
// For use inside bash scripts on Windows
}Why This Fix Was Needed:
Git Bash on Windows expects Unix-style paths, not Windows paths:
- Wrong:
bash.exe C:\Users\foo\script.sh→ Git Bash can't interpretC:\Users - Correct:
bash.exe /c/Users/foo/script.sh→ Git Bash understands/c/Users
Additionally, paths embedded inside bash scripts must use Unix format:
- Wrong:
echo "text" > C:\path\file.txt→ Backslash escapes the next character - Correct:
echo "text" > /c/path/file.txt→ Works correctly
The fix addresses both:
- Script path passed to
bash.exe(engine code) - Paths inside script content (test helper function)
Key Points:
- Automatically detects Git Bash at standard Windows installation paths
- Falls back to
bash.exein PATH if not at standard locations - Converts Windows paths (
C:\) to Git Bash format (/c/) - Test helpers ensure script content uses bash-compatible paths
.shfiles are explicitly invoked through bash on Windows- Maintains cross-platform compatibility (no changes needed on Unix)
3. Claude CLI Routing Tests - JSON Path Escaping
Problem: All 10 Claude CLI routing tests failed on Windows due to invalid JSON in .claude/settings.json. Windows paths contain backslashes (C:\Users\...), which were inserted into JSON strings without escaping, creating invalid escape sequences like \U and \A.
Status: ✅ FIXED - All 10 routing tests now pass on Windows
Affected Tests:
cupcake-core/tests/claude_code_routing_test.rs- All 10 routing tests
Root Cause:
Test code generated JSON with unescaped backslashes:
let command = "cargo run --manifest-path C:\\Users\\Admin\\cupcake\\Cargo.toml -- eval";
let settings = format!(r#"{{"command": "{command}"}}"#);This created invalid JSON:
{ "command": "cargo run --manifest-path C:UsersAdmincupcakeCargo.toml -- eval" }Claude CLI silently failed to parse the malformed JSON, so hooks never executed.
Solution Implemented:
Escape backslashes in JSON (lines 198, 550, 730):
// Escape backslashes for JSON on Windows let command_escaped = command.replace('\\', "\\\\"); let settings = format!( r#"{{ "hooks": {{ "UserPromptSubmit": [{{ "hooks": [{{ "type": "command", "command": "{command_escaped}", "timeout": 120, "env": {{ "CUPCAKE_DEBUG_ROUTING": "1", "RUST_LOG": "info" }} }}] }}] }} }}"# );- Increased hook completion wait time (line 283):
// Wait longer on Windows for hooks to complete std::thread::sleep(std::time::Duration::from_secs(5));
Test Results:
running 10 tests
test test_multiple_events_routing ... ok
test test_notification_routing ... ok
test test_posttooluse_routing ... ok
test test_precompact_routing ... ok
test test_pretooluse_routing ... ok
test test_sessionstart_routing ... ok
test test_stop_routing ... ok
test test_subagentstop_routing ... ok
test test_userpromptsubmit_routing ... ok
test test_wildcard_policy_routing ... ok
test result: ok. 10 passed; 0 failed; 0 ignored; 0 measured; 0 filtered outKey Points:
- Windows paths MUST be escaped when embedded in JSON strings
- Claude CLI silently ignores malformed JSON (no error output)
- Always use
serde_jsonfor JSON generation in production code - Test infrastructure can use manual escaping for simple cases
- This was the last blocker for full Windows test coverage
See Also: .github/WINDOWS_ROUTING_TESTS_DEBUG.md for detailed debugging history
4. Path Separators and JSON Escaping
Critical: Windows paths use backslashes which have special meaning in multiple contexts:
JSON Strings: Backslashes are escape characters
- Must use
\\in JSON:"C:\\Users\\path" - Use
command.replace('\\', "\\\\")before inserting into JSON - Or use
serde_jsonwhich handles escaping automatically
- Must use
OPA Input: OPA expects forward slashes
- Always convert to forward slashes before passing to OPA
- Use
path.replace('\\', "/")for OPA paths - Use
Path::join()for filesystem operations (handles platform differences)
- Git Bash: Expects Unix-style paths
- Convert
C:\Users\pathto/c/Users/path - See "Shell Script Tests on Windows" section above
- Convert
Key Rule: Never use string concatenation for paths - always use Path::join()
5. OPA Installation
The CI workflow installs OPA v1.7.1 on all platforms:
Windows PowerShell:
Invoke-WebRequest -Uri "https://github.com/open-policy-agent/opa/releases/download/v1.7.1/opa_windows_amd64.exe" -OutFile "opa.exe"
Move-Item opa.exe "C:\Windows\System32\opa.exe" -Force
opa versionUnix (Linux/macOS):
curl -L -o opa https://github.com/open-policy-agent/opa/releases/download/v1.7.1/opa_linux_amd64_static
chmod +x opa
sudo mv opa /usr/local/bin/
opa version6. Drive Letters and Cross-Drive Issues
Critical: GitHub Actions Windows runners use D:\a\... for the working directory but temp files are on C:\Users\...
Root-relative paths (\path) resolve to the current process's drive, NOT the file's drive:
- Process on
D:accessing\Users\...becomesD:\Users\...❌ - This is why we need full
file:///C:/Users/...URLs for OPA
Testing Windows Changes
To test Windows-specific changes locally without a Windows machine:
- The CI must be used for validation (10-minute feedback loop)
- Use extensive
eprintln!()debugging (tracing doesn't work in tests) - Verify both the happy path AND error messages contain correct paths
Debugging Tips
- Use
eprintln!()notdebug!(): Tracing is not initialized in tests - Check actual paths: Use
eprintln!("[DEBUG] Path: {:?}", path)liberally - Verify OPA version: Ensure v1.7.1+ is installed (has
opa versionin CI) - Check working directory:
std::env::current_dir()shows where the process runs - Inspect temp files: Debug output shows temp directory paths
Lessons Learned from Windows CI Debugging
1. Systematic Problem-Solving Approach
Key Principle: Understand the entire system end-to-end before deciding to skip tests or apply workarounds.
Process That Works:
- Analyze the Error: Don't just read the error message - understand what the system is trying to do
- Trace Execution Flow: Follow code from discovery → storage → execution to find where platform differences matter
- Identify Root Cause: Distinguish between product bugs, test infrastructure issues, and external dependencies
- Apply Appropriate Fix: Product bugs need runtime fixes; test issues may need platform-specific handling
Example from This Session:
- Initial Symptom: "Action did not execute" on Windows
- Wrong Approach: Skip tests because "Windows doesn't support shell scripts"
- Correct Approach:
- Traced how actions are discovered (stored as Windows paths)
- Found where they're executed (passed to
bash.exe) - Identified the mismatch (Git Bash needs Unix-style paths)
- Applied targeted fix (convert paths at execution time)
2. Understanding Platform-Specific Execution Models
Three Distinct Execution Patterns Emerged:
Pattern A: Product Features (Actions & Signals)
- What: User-authored
.shscripts that Cupcake executes - Storage: Full Windows paths (
C:\Users\...\script.sh) - Execution: Convert to Git Bash format at runtime (
/c/Users/.../script.sh) - Why: Users need to write portable scripts on Windows
- Fix Location: Engine code (
mod.rs,rulebook.rs)
Pattern B: Test Infrastructure (Claude CLI Integration)
- What: External tool (Claude CLI) installed by npm
- Storage: Platform-specific (
.ps1on Windows, binary on Unix) - Execution: JSON configuration with escaped paths on Windows
- Why: JSON parsing requires proper escaping of backslashes
- Fix Location: Test code (
claude_code_routing_test.rs) - Critical Fix: Escape Windows paths before embedding in JSON (
command.replace('\\', "\\\\"))
Pattern C: Test Helpers (Path Generation)
- What: Paths embedded inside bash script content
- Storage: N/A (generated dynamically in tests)
- Execution: Must use Unix format for Git Bash to interpret correctly
- Why: Backslashes in bash scripts are escape characters
- Fix Location: Test utility functions (
path_for_bash())
3. Git Bash Path Conversion Rules
Critical Understanding: Git Bash on Windows requires TWO types of path conversion:
Script Path (passed as argument to
bash.exe):// WRONG: bash.exe C:\Users\foo\script.sh // RIGHT: bash.exe /c/Users/foo/script.sh- Script Content (paths inside the bash script):
# WRONG: echo "text" > C:\path\file.txt # Backslash escapes next char # RIGHT: echo "text" > /c/path/file.txt # Works correctly
Key Insight: Both conversions are necessary. Fixing only one causes subtle failures.
4. Error Code 193 - Diagnostic Pattern
Meaning: "Not a valid Win32 application" - attempted to execute a file that isn't a binary executable.
Common Causes:
- Trying to execute PowerShell scripts (
.ps1) directly - Trying to execute batch files (
.bat,.cmd) withoutcmd.exe - Trying to execute shell scripts (
.sh) withoutbash.exe
Solution Pattern:
if cfg!(windows) && path.ends_with(".ps1") {
Command::new("powershell.exe").args(["-File", path, ...])
} else if cfg!(windows) && path.ends_with(".sh") {
Command::new("bash.exe").arg(convert_to_unix_path(path))
} else {
Command::new(path) // Unix: execute directly
}5. When to Skip vs. When to Fix
Skip Tests When:
- Testing platform-specific behavior that doesn't exist on target platform
- Timing-sensitive tests where platform performance differs significantly
- Tests rely on platform-specific tools not available in CI (e.g.,
/bin/echo)
Fix Tests When:
- Core functionality should work cross-platform with appropriate wrappers
- External dependencies have different installation formats but same interface
- Platform differences can be abstracted with conditional compilation
Decision Matrix:
| Scenario | Skip or Fix? | Reason |
| Action async timing | Skip | Git Bash slower than native shell, timeouts unreliable |
| Action execution | Fix | Core feature must work on all platforms |
| Signal execution | Fix | Core feature must work on all platforms |
| Claude CLI integration | Fix | External tool, platform-specific install, same interface |
Unix-specific paths (/bin/echo) | Skip | Platform-specific behavior being tested |
6. Documentation as Understanding
Pattern: Document not just the fix, but the why behind it.
Good Documentation Includes:
- Problem statement with error messages
- Root cause analysis (not just symptoms)
- Why this solution was chosen over alternatives
- Why this differs from similar-looking problems
- Code examples showing wrong vs. right approaches
Example from This Session:
**Why This Is Different from Actions/Signals**:
- Actions/signals are **product features** - users write `.sh` scripts
- Claude routing tests are **integration tests** - verify external tool integration
- Claude CLI installation format is platform-specific, outside our control7. Debugging Windows Without a Windows Machine
Techniques Used Successfully:
Liberal
eprintln!()Statements:eprintln!("[DEBUG] Path: {:?}", path); eprintln!("[DEBUG] Executing: {command}"); eprintln!("[DEBUG] Exit code: {:?}", output.status.code());CI as Validation Loop:
- Push changes, wait ~10 minutes for CI feedback
- Read stderr/stdout from failed tests
- Iterate based on actual Windows behavior
Extensive Error Context:
.expect(&format!("Failed to execute: {}", command)) // Better than just: .expect("Failed to execute")- Path Inspection at Every Stage:
- Log paths when discovered
- Log paths when stored
- Log paths before execution
- Log paths inside script content
Key Insight: Cannot guess Windows behavior - must observe actual execution through CI logs.
8. Silent Failures in External Tools
Lesson from Claude CLI Routing Tests:
External tools may silently ignore malformed configuration files, making debugging extremely difficult.
The Issue:
- Windows paths in JSON must have escaped backslashes:
"C:\\Users\\path" - Test code inserted unescaped paths:
"C:\Users\path"(invalid JSON) - Claude CLI silently failed to parse JSON - no error output, exit code 0
- Hooks never executed, but no indication why
Debugging Symptoms:
- Process succeeds (exit code 0) ✅
- No stderr output ✅
- Expected side effects don't happen ❌
- Leads to investigating the wrong components (Cupcake instead of JSON config)
Solution Pattern:
// BAD: Manual string formatting
let path = "C:\\Users\\path";
let json = format!(r#"{{"path": "{path}"}}"#); // Invalid JSON!
// GOOD: Use serde_json for automatic escaping
use serde_json::json;
let config = json!({
"path": path // Automatically escaped
});When Debugging Silent Failures:
- Validate all generated config files (JSON, YAML, etc.)
- Check for platform-specific character escaping issues
- Test with minimal reproducible config outside the main application
- Add explicit validation/parsing in test setup if possible
9. Cross-Platform Test Helpers Pattern
Best Practice: Create platform-aware helper functions for test setup:
#[cfg(windows)]
fn path_for_bash(path: &PathBuf) -> String {
// Convert C:\path → /c/path for Git Bash
}
#[cfg(not(windows))]
fn path_for_bash(path: &PathBuf) -> String {
path.display().to_string() // No conversion needed
}Why This Works:
- Tests remain readable (same API on all platforms)
- Platform logic isolated in helper
- Easy to update if conversion rules change
- Compiler ensures correct version used
10. The "Script vs. Binary" Distinction
Critical Insight: On Windows, there are THREE types of executables:
- Native Binaries (
.exe): Execute directly withCommand::new(path) - Scripts (
.sh,.ps1,.bat): Require interpreter wrapper - Shebang Scripts (Unix only): OS reads
#!/bin/bashand routes to interpreter
Windows Behavior:
- No shebang support - must detect script type by extension
- Each script type needs specific interpreter
- Interpreter location varies (Git Bash in Program Files, PowerShell in System32)
Design Pattern:
fn execute_script(path: &str) -> Command {
if cfg!(windows) {
if path.ends_with(".ps1") {
Command::new("powershell.exe")
.args(["-ExecutionPolicy", "Bypass", "-File", path])
} else if path.ends_with(".sh") {
let bash_path = convert_to_unix_path(path);
Command::new(*SHELL_COMMAND).arg(bash_path)
} else {
Command::new(path) // Assume .exe
}
} else {
Command::new(path) // Unix handles shebang
}
}Future Improvements
- Consider using a Rust tar library instead of OPA's
-oflag to avoid path issues entirely - File an upstream issue with OPA about Windows path handling if not already tracked
- Add Windows-specific integration tests that verify path handling
- Document workarounds for any new OPA-related features on Windows
- Create reusable test utilities for cross-platform script execution in tests
- Consider GitHub Actions Windows runner documentation for temp file locations and drive mappings
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

