Skip to content

Commit e7e1002

Browse files
authored
Fix/claude code setup (#84)
* fix(examples): Fix Claude Code setup.sh to match Cursor pattern - Remove manual `opa build` command that fails due to missing helpers dir - Let `cupcake init --harness claude` create settings.json (already does this) - Replace `cargo run` with built binary path for better performance - Remove uv dependency check (not needed for this example) - Update hooks.json with full binary path via sed (matches cursor setup) WASM compilation is handled automatically by `cupcake eval` at runtime, so no manual opa build step is needed. * docs: Add Policy Studio links to navigation - Add "Policy Studio (Desktop only)" to main sidebar nav - Add "See Examples" under Getting Started > Usage
1 parent 0a1a196 commit e7e1002

3 files changed

Lines changed: 56 additions & 174 deletions

File tree

docs/zensical.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ nav = [
4848
{ "Home" = "index.md" },
4949
{ "Why Rego" = "why-rego.md" },
5050
{ "Security Disclaimer" = "security-disclaimer.md" },
51+
{ "Policy Studio (Desktop only)" = "https://cupcake-policy-studio.vercel.app/example-policies/security/protecting-paths?harness=claude-code&format=rego" },
5152
{ "Getting Started" = [
5253
"getting-started/installation.md",
5354
{ "Usage" = [
@@ -57,6 +58,7 @@ nav = [
5758
"getting-started/usage/opencode.md",
5859
"getting-started/usage/factory-ai.md",
5960
] },
61+
{ "See Examples" = "https://cupcake-policy-studio.vercel.app/example-policies/security/protecting-paths?harness=claude-code&format=rego" },
6062
] },
6163
{ "Reference" = [
6264
{ "Policies" = [

examples/claude-code/0_Welcome/setup.ps1

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# PowerShell script for Windows setup
22
# Run with: powershell -ExecutionPolicy Bypass -File setup.ps1
33

4-
Write-Host "Cupcake Evaluation Setup (Windows)" -ForegroundColor Green
5-
Write-Host "==================================`n"
4+
Write-Host "Cupcake Claude Code Evaluation Setup (Windows)" -ForegroundColor Green
5+
Write-Host "===============================================`n"
66

77
# Check if Rust/Cargo is installed
88
try {
@@ -25,15 +25,8 @@ try {
2525
exit 1
2626
}
2727

28-
# Check if OPA is installed
29-
try {
30-
$uvVersion = uv --version 2>$null
31-
Write-Host "✅ uv found: $uvVersion" -ForegroundColor Green
32-
} catch {
33-
Write-Host "❌ uv not found in PATH. Please install OPA:" -ForegroundColor Red
34-
Write-Host " https://docs.astral.sh/uv/" -ForegroundColor Yellow
35-
exit 1
36-
}
28+
# Save current directory
29+
$originalDir = Get-Location
3730

3831
# Build Cupcake binary
3932
Write-Host "`nBuilding Cupcake binary..."
@@ -46,23 +39,31 @@ if ($LASTEXITCODE -ne 0) {
4639
}
4740
Write-Host "✅ Build complete" -ForegroundColor Green
4841

49-
# Add to PATH for this session
50-
$cupcakePath = Join-Path (Get-Location) "target\release"
51-
$env:PATH = "$cupcakePath;$env:PATH"
52-
Write-Host "✅ Added cupcake to PATH for this session" -ForegroundColor Green
42+
# Store the cupcake binary path
43+
$cupcakeBin = Join-Path (Get-Location) "target\release\cupcake.exe"
44+
Write-Host "✅ Using cupcake binary at: $cupcakeBin" -ForegroundColor Green
5345

54-
# Return to eval directory
55-
Pop-Location
46+
# Return to original directory
47+
Set-Location $originalDir
5648

57-
# Initialize Cupcake project
58-
Write-Host "`nInitializing Cupcake project..."
59-
cupcake init --harness claude
49+
# Initialize Cupcake project using the explicit path
50+
Write-Host "`nInitializing Cupcake project with Claude Code harness..."
51+
& $cupcakeBin init --harness claude
6052
if ($LASTEXITCODE -ne 0) {
6153
Write-Host "❌ Project initialization failed" -ForegroundColor Red
6254
exit 1
6355
}
6456
Write-Host "✅ Project initialized" -ForegroundColor Green
6557

58+
# Update settings.json to use the full path to the cupcake binary
59+
# This ensures Claude Code can find cupcake even if it's not in PATH
60+
Write-Host "`nUpdating settings.json with full binary path..."
61+
$settingsPath = ".claude\settings.json"
62+
$settingsContent = Get-Content $settingsPath -Raw
63+
$settingsContent = $settingsContent -replace "cupcake eval", "$cupcakeBin eval"
64+
$settingsContent | Out-File -FilePath $settingsPath -Encoding UTF8 -NoNewline
65+
Write-Host "✅ Hooks configured with: $cupcakeBin" -ForegroundColor Green
66+
6667
# Copy example policies to Claude Code policies directory
6768
Write-Host "`nCopying example policies..."
6869
Copy-Item -Path "..\..\fixtures\security_policy.rego" -Destination ".cupcake\policies\claude\" -Force
@@ -72,85 +73,18 @@ Write-Host "✅ Example policies copied" -ForegroundColor Green
7273

7374
Write-Host "✅ Builtins configured (protected_paths, git_pre_check, rulebook_security_guardrails)" -ForegroundColor Green
7475

75-
# Compile policies to WASM (Claude Code policies + shared helpers)
76-
Write-Host "`nCompiling Claude Code policies to WASM..."
77-
opa build -t wasm -e cupcake/system/evaluate .cupcake/policies/claude/ .cupcake/policies/helpers/
78-
if ($LASTEXITCODE -ne 0) {
79-
Write-Host "❌ Policy compilation failed" -ForegroundColor Red
80-
exit 1
81-
}
82-
Write-Host "✅ Policies compiled to bundle.tar.gz" -ForegroundColor Green
83-
84-
# Create Claude Code settings directory and hooks integration
85-
Write-Host "`nSetting up Claude Code hooks integration..."
86-
New-Item -ItemType Directory -Force -Path ".claude" | Out-Null
87-
88-
# Get absolute paths
89-
$manifestPath = Resolve-Path "..\..\..\Cargo.toml"
90-
$opaDir = Split-Path (Get-Command opa).Source -Parent
91-
92-
# Create Claude Code settings with Windows paths
93-
$settingsContent = @"
94-
{
95-
"hooks": {
96-
"PreToolUse": [
97-
{
98-
"matcher": "*",
99-
"hooks": [
100-
{
101-
"type": "command",
102-
"command": "cargo run --manifest-path `"$manifestPath`" -- eval --harness claude --log-level info --debug-files",
103-
"timeout": 120,
104-
"env": {
105-
"PATH": "$opaDir;%PATH%"
106-
}
107-
}
108-
]
109-
}
110-
],
111-
"PostToolUse": [
112-
{
113-
"matcher": "*",
114-
"hooks": [
115-
{
116-
"type": "command",
117-
"command": "cargo run --manifest-path `"$manifestPath`" -- eval --harness claude --log-level info --debug-files",
118-
"timeout": 120,
119-
"env": {
120-
"PATH": "$opaDir;%PATH%"
121-
}
122-
}
123-
]
124-
}
125-
],
126-
"UserPromptSubmit": [
127-
{
128-
"hooks": [
129-
{
130-
"type": "command",
131-
"command": "cargo run --manifest-path `"$manifestPath`" -- eval --harness claude --log-level info --debug-files",
132-
"timeout": 120,
133-
"env": {
134-
"PATH": "$opaDir;%PATH%"
135-
}
136-
}
137-
]
138-
}
139-
]
140-
}
141-
}
142-
"@
143-
144-
$settingsContent | Out-File -FilePath ".claude\settings.json" -Encoding UTF8
145-
Write-Host "✅ Claude Code hooks configured" -ForegroundColor Green
76+
# Note: WASM compilation is handled automatically by 'cupcake eval' at runtime
77+
# No manual 'opa build' step needed - cupcake compiles policies including helpers
14678

14779
Write-Host "`n🎉 Setup complete!" -ForegroundColor Green
14880
Write-Host "`nNext steps:" -ForegroundColor Cyan
14981
Write-Host "1. Add cupcake to your PATH:" -ForegroundColor White
15082
Write-Host " `$env:PATH = `"$(Resolve-Path ..\..\..\target\release);`$env:PATH`"" -ForegroundColor Yellow
151-
Write-Host "2. Start Claude Code in this directory" -ForegroundColor White
83+
Write-Host "2. Open this directory in Claude Code" -ForegroundColor White
15284
Write-Host "3. Try running commands that trigger policies" -ForegroundColor White
153-
Write-Host "`nExample commands to test:" -ForegroundColor Cyan
85+
Write-Host "`nManual testing with test events:" -ForegroundColor Cyan
86+
Write-Host " cupcake eval --harness claude < test-events/shell-rm.json" -ForegroundColor Yellow
87+
Write-Host "`nExample commands to test in Claude Code:" -ForegroundColor Cyan
15488
Write-Host "- ls (safe, should work)" -ForegroundColor White
15589
Write-Host "- Remove-Item -Recurse -Force C:\temp\test (dangerous, should block)" -ForegroundColor White
15690
Write-Host "- Edit C:\Windows\System32\drivers\etc\hosts (system file, should block)" -ForegroundColor White
Lines changed: 27 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/bash
22
set -e
33

4-
echo "Cupcake Evaluation Setup"
5-
echo "=========================="
4+
echo "Cupcake Claude Code Evaluation Setup"
5+
echo "====================================="
66

77
# Check if Rust/Cargo is installed
88
if ! command -v cargo &> /dev/null; then
@@ -22,14 +22,8 @@ else
2222
echo "✅ OPA found: $(opa version | head -n1)"
2323
fi
2424

25-
# Check if uv is installed
26-
if ! command -v uv &> /dev/null; then
27-
echo "❌ uv not found in PATH. Please install uv:"
28-
echo " https://docs.astral.sh/uv/"
29-
exit 1
30-
else
31-
echo "✅ uv found: $(uv --version)"
32-
fi
25+
# Save current directory to return to later
26+
ORIGINAL_DIR="$(pwd)"
3327

3428
# Build Cupcake binary
3529
echo "Building Cupcake binary..."
@@ -41,14 +35,26 @@ echo "✅ Build complete"
4135
CUPCAKE_BIN="$(pwd)/target/release/cupcake"
4236
echo "✅ Using cupcake binary at: $CUPCAKE_BIN"
4337

44-
# Return to examples directory
45-
cd examples/claude-code/0_Welcome
38+
# Return to original directory
39+
cd "$ORIGINAL_DIR"
4640

4741
# Initialize Cupcake project using the explicit path
48-
echo "Initializing Cupcake project..."
42+
echo "Initializing Cupcake project with Claude Code harness..."
4943
"$CUPCAKE_BIN" init --harness claude
5044
echo "✅ Project initialized"
5145

46+
# Update settings.json to use the full path to the cupcake binary
47+
# This ensures Claude Code can find cupcake even if it's not in PATH
48+
echo "Updating settings.json with full binary path..."
49+
if [[ "$OSTYPE" == "darwin"* ]]; then
50+
# macOS uses BSD sed
51+
sed -i '' "s|cupcake eval|$CUPCAKE_BIN eval|g" .claude/settings.json
52+
else
53+
# Linux uses GNU sed
54+
sed -i "s|cupcake eval|$CUPCAKE_BIN eval|g" .claude/settings.json
55+
fi
56+
echo "✅ Hooks configured with: $CUPCAKE_BIN"
57+
5258
# Copy example policies to Claude Code policies directory
5359
echo "Copying example policies..."
5460
cp ../../fixtures/security_policy.rego .cupcake/policies/claude/
@@ -59,83 +65,23 @@ echo "✅ Example policies copied"
5965
# Builtins are now pre-configured in the base template
6066
echo "✅ Builtins configured (protected_paths, git_pre_check, rulebook_security_guardrails)"
6167

62-
# Compile policies to WASM (Claude Code policies + shared helpers)
63-
echo "Compiling Claude Code policies to WASM..."
64-
opa build -t wasm -e cupcake/system/evaluate .cupcake/policies/claude/ .cupcake/policies/helpers/
65-
echo "✅ Policies compiled to bundle.tar.gz"
66-
67-
# Create Claude Code settings directory and hooks integration
68-
echo "Setting up Claude Code hooks integration..."
69-
mkdir -p .claude
70-
71-
# Create Claude Code settings with direct cargo command (like working demo)
72-
MANIFEST_PATH="$(realpath ../../../Cargo.toml)"
73-
OPA_DIR="$(dirname "$(which opa)")"
74-
75-
cat > .claude/settings.json << EOF
76-
{
77-
"hooks": {
78-
"PreToolUse": [
79-
{
80-
"matcher": "*",
81-
"hooks": [
82-
{
83-
"type": "command",
84-
"command": "cargo run --manifest-path $MANIFEST_PATH -- eval --harness claude --log-level info --debug-files",
85-
"timeout": 120,
86-
"env": {
87-
"PATH": "$OPA_DIR:\$PATH"
88-
}
89-
}
90-
]
91-
}
92-
],
93-
"PostToolUse": [
94-
{
95-
"matcher": "*",
96-
"hooks": [
97-
{
98-
"type": "command",
99-
"command": "cargo run --manifest-path $MANIFEST_PATH -- eval --harness claude --log-level info --debug-files",
100-
"timeout": 120,
101-
"env": {
102-
"PATH": "$OPA_DIR:\$PATH"
103-
}
104-
}
105-
]
106-
}
107-
],
108-
"UserPromptSubmit": [
109-
{
110-
"hooks": [
111-
{
112-
"type": "command",
113-
"command": "cargo run --manifest-path $MANIFEST_PATH -- eval --harness claude --log-level info --debug-files",
114-
"timeout": 120,
115-
"env": {
116-
"PATH": "$OPA_DIR:\$PATH"
117-
}
118-
}
119-
]
120-
}
121-
]
122-
}
123-
}
124-
EOF
125-
126-
echo "✅ Claude Code hooks configured"
68+
# Note: WASM compilation is handled automatically by 'cupcake eval' at runtime
69+
# No manual 'opa build' step needed - cupcake compiles policies including helpers
12770

12871
echo ""
12972
echo "🎉 Setup complete!"
13073
echo ""
13174
echo "Next steps:"
13275
echo "1. To add cupcake to your PATH, run:"
13376
echo " export PATH=\"$(realpath ../../../target/release):\$PATH\""
134-
echo "2. Start Claude Code in this directory"
77+
echo "2. Open this directory in Claude Code"
13578
echo "3. Try running commands that trigger policies"
13679
echo ""
137-
echo "Example commands to test:"
138-
echo "- ls -la (safe, should work)"
80+
echo "Manual testing with test events:"
81+
echo " cupcake eval --harness claude < test-events/shell-rm.json"
82+
echo ""
83+
echo "Example commands to test in Claude Code:"
84+
echo "- ls -la (safe, should work)"
13985
echo "- rm -rf /tmp/test (dangerous, should block)"
14086
echo "- Edit /etc/hosts (system file, should block)"
14187
echo "- git push --force (risky, should ask)"

0 commit comments

Comments
 (0)