mirror of
https://github.com/leonvanzyl/autocoder.git
synced 2026-01-30 06:12:06 +00:00
Implement hierarchical command security with project and org-level configs:
WHAT'S NEW:
- Project-level YAML config (.autocoder/allowed_commands.yaml)
- Organization-level config (~/.autocoder/config.yaml)
- Pattern matching (exact, wildcards, local scripts)
- Hardcoded blocklist (sudo, dd, shutdown - never allowed)
- Org blocklist (terraform, kubectl - configurable)
- Helpful error messages with config hints
- Comprehensive documentation and examples
ARCHITECTURE:
- Hierarchical resolution: Hardcoded → Org Block → Org Allow → Global → Project
- YAML validation with 50 command limit per project
- Pattern matching: exact ("swift"), wildcards ("swift*"), scripts ("./build.sh")
- Secure by default: all examples commented out
TESTING:
- 136 unit tests (pattern matching, YAML, hierarchy, validation)
- 9 integration tests (real security hook flows)
- All tests passing, 100% backward compatible
DOCUMENTATION:
- examples/README.md - comprehensive guide with use cases
- examples/project_allowed_commands.yaml - template (all commented)
- examples/org_config.yaml - org config template (all commented)
- PHASE3_SPEC.md - mid-session approval spec (future enhancement)
- Updated CLAUDE.md with security model documentation
USE CASES:
- iOS projects: Add Swift toolchain (xcodebuild, swift*, etc.)
- Rust projects: Add cargo, rustc, clippy
- Enterprise: Block aws, kubectl, terraform org-wide
- Custom scripts: Allow ./scripts/build.sh
PHASES:
✅ Phase 1: Project YAML + blocklist (implemented)
✅ Phase 2: Org config + hierarchy (implemented)
📋 Phase 3: Mid-session approval (spec ready, not implemented)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
293 lines
9.9 KiB
Python
293 lines
9.9 KiB
Python
"""
|
|
Prompt Loading Utilities
|
|
========================
|
|
|
|
Functions for loading prompt templates with project-specific support.
|
|
|
|
Fallback chain:
|
|
1. Project-specific: {project_dir}/prompts/{name}.md
|
|
2. Base template: .claude/templates/{name}.template.md
|
|
"""
|
|
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
# Base templates location (generic templates)
|
|
TEMPLATES_DIR = Path(__file__).parent / ".claude" / "templates"
|
|
|
|
|
|
def get_project_prompts_dir(project_dir: Path) -> Path:
|
|
"""Get the prompts directory for a specific project."""
|
|
return project_dir / "prompts"
|
|
|
|
|
|
def load_prompt(name: str, project_dir: Path | None = None) -> str:
|
|
"""
|
|
Load a prompt template with fallback chain.
|
|
|
|
Fallback order:
|
|
1. Project-specific: {project_dir}/prompts/{name}.md
|
|
2. Base template: .claude/templates/{name}.template.md
|
|
|
|
Args:
|
|
name: The prompt name (without extension), e.g., "initializer_prompt"
|
|
project_dir: Optional project directory for project-specific prompts
|
|
|
|
Returns:
|
|
The prompt content as a string
|
|
|
|
Raises:
|
|
FileNotFoundError: If prompt not found in any location
|
|
"""
|
|
# 1. Try project-specific first
|
|
if project_dir:
|
|
project_prompts = get_project_prompts_dir(project_dir)
|
|
project_path = project_prompts / f"{name}.md"
|
|
if project_path.exists():
|
|
try:
|
|
return project_path.read_text(encoding="utf-8")
|
|
except (OSError, PermissionError) as e:
|
|
print(f"Warning: Could not read {project_path}: {e}")
|
|
|
|
# 2. Try base template
|
|
template_path = TEMPLATES_DIR / f"{name}.template.md"
|
|
if template_path.exists():
|
|
try:
|
|
return template_path.read_text(encoding="utf-8")
|
|
except (OSError, PermissionError) as e:
|
|
print(f"Warning: Could not read {template_path}: {e}")
|
|
|
|
raise FileNotFoundError(
|
|
f"Prompt '{name}' not found in:\n"
|
|
f" - Project: {project_dir / 'prompts' if project_dir else 'N/A'}\n"
|
|
f" - Templates: {TEMPLATES_DIR}"
|
|
)
|
|
|
|
|
|
def get_initializer_prompt(project_dir: Path | None = None) -> str:
|
|
"""Load the initializer prompt (project-specific if available)."""
|
|
return load_prompt("initializer_prompt", project_dir)
|
|
|
|
|
|
def get_coding_prompt(project_dir: Path | None = None) -> str:
|
|
"""Load the coding agent prompt (project-specific if available)."""
|
|
return load_prompt("coding_prompt", project_dir)
|
|
|
|
|
|
def get_testing_prompt(project_dir: Path | None = None) -> str:
|
|
"""Load the testing agent prompt (project-specific if available)."""
|
|
return load_prompt("testing_prompt", project_dir)
|
|
|
|
|
|
def get_single_feature_prompt(feature_id: int, project_dir: Path | None = None, yolo_mode: bool = False) -> str:
|
|
"""
|
|
Load the coding prompt with single-feature focus instructions prepended.
|
|
|
|
When the orchestrator assigns a specific feature to a coding agent,
|
|
this prompt ensures the agent works ONLY on that feature.
|
|
|
|
Args:
|
|
feature_id: The specific feature ID to work on
|
|
project_dir: Optional project directory for project-specific prompts
|
|
yolo_mode: Ignored (kept for backward compatibility). Testing is now
|
|
handled by separate testing agents, not YOLO prompts.
|
|
|
|
Returns:
|
|
The prompt with single-feature instructions prepended
|
|
"""
|
|
# Always use the standard coding prompt
|
|
# (Testing/regression is handled by separate testing agents)
|
|
base_prompt = get_coding_prompt(project_dir)
|
|
|
|
# Prepend single-feature instructions
|
|
single_feature_header = f"""## SINGLE FEATURE MODE
|
|
|
|
**CRITICAL: You are assigned to work on Feature #{feature_id} ONLY.**
|
|
|
|
This session is part of a parallel execution where multiple agents work on different features simultaneously. You MUST:
|
|
|
|
1. **Skip the `feature_get_next` step** - Your feature is already assigned: #{feature_id}
|
|
2. **Immediately mark feature #{feature_id} as in-progress** using `feature_mark_in_progress`
|
|
3. **Focus ONLY on implementing and testing feature #{feature_id}**
|
|
4. **Do NOT work on any other features** - other agents are handling them
|
|
|
|
When you complete feature #{feature_id}:
|
|
- Mark it as passing with `feature_mark_passing`
|
|
- Commit your changes
|
|
- End the session
|
|
|
|
If you cannot complete feature #{feature_id} due to a blocker:
|
|
- Use `feature_skip` to move it to the end of the queue
|
|
- Document the blocker in claude-progress.txt
|
|
- End the session
|
|
|
|
---
|
|
|
|
"""
|
|
|
|
return single_feature_header + base_prompt
|
|
|
|
|
|
def get_app_spec(project_dir: Path) -> str:
|
|
"""
|
|
Load the app spec from the project.
|
|
|
|
Checks in order:
|
|
1. Project prompts directory: {project_dir}/prompts/app_spec.txt
|
|
2. Project root (legacy): {project_dir}/app_spec.txt
|
|
|
|
Args:
|
|
project_dir: The project directory
|
|
|
|
Returns:
|
|
The app spec content
|
|
|
|
Raises:
|
|
FileNotFoundError: If no app_spec.txt found
|
|
"""
|
|
# Try project prompts directory first
|
|
project_prompts = get_project_prompts_dir(project_dir)
|
|
spec_path = project_prompts / "app_spec.txt"
|
|
if spec_path.exists():
|
|
try:
|
|
return spec_path.read_text(encoding="utf-8")
|
|
except (OSError, PermissionError) as e:
|
|
raise FileNotFoundError(f"Could not read {spec_path}: {e}") from e
|
|
|
|
# Fallback to legacy location in project root
|
|
legacy_spec = project_dir / "app_spec.txt"
|
|
if legacy_spec.exists():
|
|
try:
|
|
return legacy_spec.read_text(encoding="utf-8")
|
|
except (OSError, PermissionError) as e:
|
|
raise FileNotFoundError(f"Could not read {legacy_spec}: {e}") from e
|
|
|
|
raise FileNotFoundError(f"No app_spec.txt found for project: {project_dir}")
|
|
|
|
|
|
def scaffold_project_prompts(project_dir: Path) -> Path:
|
|
"""
|
|
Create the project prompts directory and copy base templates.
|
|
|
|
This sets up a new project with template files that can be customized.
|
|
|
|
Args:
|
|
project_dir: The absolute path to the project directory
|
|
|
|
Returns:
|
|
The path to the project prompts directory
|
|
"""
|
|
project_prompts = get_project_prompts_dir(project_dir)
|
|
project_prompts.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Create .autocoder directory for configuration files
|
|
autocoder_dir = project_dir / ".autocoder"
|
|
autocoder_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Define template mappings: (source_template, destination_name)
|
|
templates = [
|
|
("app_spec.template.txt", "app_spec.txt"),
|
|
("coding_prompt.template.md", "coding_prompt.md"),
|
|
("initializer_prompt.template.md", "initializer_prompt.md"),
|
|
("testing_prompt.template.md", "testing_prompt.md"),
|
|
]
|
|
|
|
copied_files = []
|
|
for template_name, dest_name in templates:
|
|
template_path = TEMPLATES_DIR / template_name
|
|
dest_path = project_prompts / dest_name
|
|
|
|
# Only copy if template exists and destination doesn't
|
|
if template_path.exists() and not dest_path.exists():
|
|
try:
|
|
shutil.copy(template_path, dest_path)
|
|
copied_files.append(dest_name)
|
|
except (OSError, PermissionError) as e:
|
|
print(f" Warning: Could not copy {dest_name}: {e}")
|
|
|
|
# Copy allowed_commands.yaml template to .autocoder/
|
|
examples_dir = Path(__file__).parent / "examples"
|
|
allowed_commands_template = examples_dir / "project_allowed_commands.yaml"
|
|
allowed_commands_dest = autocoder_dir / "allowed_commands.yaml"
|
|
if allowed_commands_template.exists() and not allowed_commands_dest.exists():
|
|
try:
|
|
shutil.copy(allowed_commands_template, allowed_commands_dest)
|
|
copied_files.append(".autocoder/allowed_commands.yaml")
|
|
except (OSError, PermissionError) as e:
|
|
print(f" Warning: Could not copy allowed_commands.yaml: {e}")
|
|
|
|
if copied_files:
|
|
print(f" Created project files: {', '.join(copied_files)}")
|
|
|
|
return project_prompts
|
|
|
|
|
|
def has_project_prompts(project_dir: Path) -> bool:
|
|
"""
|
|
Check if a project has valid prompts set up.
|
|
|
|
A project has valid prompts if:
|
|
1. The prompts directory exists, AND
|
|
2. app_spec.txt exists within it, AND
|
|
3. app_spec.txt contains the <project_specification> tag
|
|
|
|
Args:
|
|
project_dir: The project directory to check
|
|
|
|
Returns:
|
|
True if valid project prompts exist, False otherwise
|
|
"""
|
|
project_prompts = get_project_prompts_dir(project_dir)
|
|
app_spec = project_prompts / "app_spec.txt"
|
|
|
|
if not app_spec.exists():
|
|
# Also check legacy location in project root
|
|
legacy_spec = project_dir / "app_spec.txt"
|
|
if legacy_spec.exists():
|
|
try:
|
|
content = legacy_spec.read_text(encoding="utf-8")
|
|
return "<project_specification>" in content
|
|
except (OSError, PermissionError):
|
|
return False
|
|
return False
|
|
|
|
# Check for valid spec content
|
|
try:
|
|
content = app_spec.read_text(encoding="utf-8")
|
|
return "<project_specification>" in content
|
|
except (OSError, PermissionError):
|
|
return False
|
|
|
|
|
|
def copy_spec_to_project(project_dir: Path) -> None:
|
|
"""
|
|
Copy the app spec file into the project root directory for the agent to read.
|
|
|
|
This maintains backwards compatibility - the agent expects app_spec.txt
|
|
in the project root directory.
|
|
|
|
The spec is sourced from: {project_dir}/prompts/app_spec.txt
|
|
|
|
Args:
|
|
project_dir: The project directory
|
|
"""
|
|
spec_dest = project_dir / "app_spec.txt"
|
|
|
|
# Don't overwrite if already exists
|
|
if spec_dest.exists():
|
|
return
|
|
|
|
# Copy from project prompts directory
|
|
project_prompts = get_project_prompts_dir(project_dir)
|
|
project_spec = project_prompts / "app_spec.txt"
|
|
if project_spec.exists():
|
|
try:
|
|
shutil.copy(project_spec, spec_dest)
|
|
print("Copied app_spec.txt to project directory")
|
|
return
|
|
except (OSError, PermissionError) as e:
|
|
print(f"Warning: Could not copy app_spec.txt: {e}")
|
|
return
|
|
|
|
print("Warning: No app_spec.txt found to copy to project directory")
|