How to Protect Critical Code
How-To Guide
Document Type: How-To Guide
Status: OUTLINE — Content to be added
Goal: Use ACP lock levels to protect important code from AI modification
Prerequisites: ACP CLI installed, basic annotation knowledge
Time: 20-30 minutes
The Problem
You have code that's critical to your application—security logic, payment processing, core algorithms—and you want to ensure AI coding assistants don't accidentally modify it.
Solution Overview
- Identify critical code categories
- Choose appropriate lock levels
- Apply annotations
- Verify protection
- Set up violation tracking (optional)
Step 1: Identify Critical Code
TODO: Add decision framework
Category 1: Security-Critical
Code that handles:
- Authentication and authorization
- Cryptographic operations
- Session management
- Access control
Recommendation: @acp:lock frozen
Category 2: Financial-Critical
Code that handles:
- Payment processing
- Billing calculations
- Financial reporting
- Audit trails
Recommendation: @acp:lock frozen or restricted
Category 3: Compliance-Critical
Code that handles:
- GDPR/privacy compliance
- Healthcare (HIPAA)
- Regulatory reporting
- Legal requirements
Recommendation: @acp:lock frozen
Category 4: Infrastructure-Critical
Code that handles:
- Database migrations
- Configuration management
- Deployment scripts
- Backup/restore
Recommendation: @acp:lock restricted
Category 5: API-Critical
Code that handles:
- Public API contracts
- SDK interfaces
- Integration points
Recommendation: @acp:lock restricted with @acp:stability stable
Step 2: Understand Lock Levels
TODO: Add detailed behavior descriptions
| Level | Meaning | AI Behavior |
|---|---|---|
frozen | Never modify | Skip entirely, don't suggest changes |
restricted | Approval required | Warn before suggesting, flag for review |
approval-required | Change review needed | Suggest with review request |
tests-required | Must have tests | Verify test coverage before changing |
docs-required | Must document | Ensure documentation exists |
review-required | Code review needed | Flag for human review |
normal | Standard code | Normal AI behavior |
experimental | Expect changes | Full AI flexibility |
Lock Level Selection Guide
Is this code security-critical?
├─ Yes → frozen
└─ No
└─ Is unauthorized change catastrophic?
├─ Yes → restricted
└─ No
└─ Does it need human review?
├─ Yes → review-required
└─ No → normalStep 3: Apply Annotations
TODO: Add language-specific examples
File-Level Protection
// src/auth/session.ts
// @acp:lock frozen - Session management, security critical
// @acp:owner security-team - All changes require security review
export class SessionService {
// ...
}Function-Level Protection
// src/utils/helpers.ts
// @acp:lock normal - General utilities
export function formatDate(date: Date): string {
// Normal code, AI can modify
}
// @acp:lock frozen - Cryptographic hash, security critical
export function hashPassword(password: string): string {
// Frozen, AI must not modify
}Block-Level Protection
// @acp:lock frozen - BEGIN payment calculation
function calculateTotal(items: Item[]): Money {
// Critical calculation logic
}
// @acp:lock frozen - ENDStep 4: Add Context with Directives
TODO: Expand with examples
The directive after the lock level explains WHY:
// @acp:lock frozen - PCI-DSS compliant, audited annually
// @acp:lock frozen - Legacy edge case handling from PROD-4521
// @acp:lock frozen - Performance optimized, benchmarked
// @acp:lock restricted - Public API, breaking change requires deprecationStep 5: Verify Protection
TODO: Add verification steps
Check Frozen Files
acp query '.constraints.by_lock_level.frozen'Check Specific File
acp constraints src/auth/session.tsExpected output:
File: src/auth/session.ts
━━━━━━━━━━━━━━━━━━━━━━━━
Lock Level: frozen
Reason: Session management, security critical
Owner: security-team
⚠️ This file should NOT be modified by AI assistants.
Contact: @security-leadTest with AI
- Open AI tool with ACP integration
- Ask: "Refactor the SessionService class"
- Expected: AI should decline or acknowledge constraint
Step 6: Track Violations (Optional)
TODO: Expand violation tracking
Enable Tracking
// .acp.config.json
{
"constraints": {
"track_violations": true,
"audit_file": ".acp/violations.log"
}
}Review Violations
cat .acp/violations.log[2024-12-17T15:30:00Z] VIOLATION: Attempted modify src/auth/session.ts (frozen)
Tool: cursor
Operation: edit
User: developer@example.comPatterns and Best Practices
TODO: Expand each pattern
Pattern 1: Frozen Core, Normal Periphery
src/payments/
├── core/
│ ├── processor.ts # frozen
│ └── calculator.ts # frozen
├── adapters/
│ ├── stripe.ts # restricted
│ └── paypal.ts # restricted
└── utils/
└── formatting.ts # normalPattern 2: Graduated Permissions
// @acp:lock frozen
export const CRITICAL_CONFIG = { ... }; // Never change
// @acp:lock restricted
export function processPayment() { ... } // Needs approval
// @acp:lock normal
export function formatReceipt() { ... } // AI can modifyPattern 3: Time-Based Locks
// @acp:lock frozen - Freeze until 2025-01-15 (audit period)
export function auditReport() { ... }Common Mistakes
TODO: Add examples for each
Mistake 1: Over-Freezing
Freezing too much code limits AI helpfulness.
Solution: Only freeze truly critical code.
Mistake 2: Missing Directives
Lock without explanation:
// @acp:lock frozenSolution: Always explain why:
// @acp:lock frozen - Handles PII, GDPR compliance requirementMistake 3: Inconsistent Levels
Same type of code with different locks.
Solution: Create a lock level policy document.
Verification Checklist
- All security-critical code is
frozen - All financial-critical code is
frozenorrestricted - All compliance-critical code is
frozen - All public APIs are
restrictedwith stability markers - Directives explain WHY for all locks
- Owners are assigned for all restricted+ code
- Cache is regenerated after annotations
- AI tool respects constraints (tested)
Related
This guide is an outline. Contribute content →