Regulatory Compliance Automation
Your company just passed its SOC 2 Type II audit, but the evidence collection took three engineers two full weeks. Half the work was copying logs from various systems into spreadsheets, writing narrative descriptions of controls, and screenshotting configurations that proved policies were enforced. Next quarter the auditor will ask for the same evidence again, and the engineer who knew where everything lived just left the company.
Compliance is not optional, but the manual labor surrounding it is. AI coding tools can automate policy enforcement at the code level, generate audit trails from your existing infrastructure, and produce compliance documentation that stays current without dedicated headcount.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- Automated compliance-as-code patterns that enforce policies in CI/CD pipelines
- Pre-commit hooks that catch compliance violations before code reaches production
- AI-driven audit trail generation from Git history, infrastructure logs, and deployment records
- Prompts for generating SOC 2, HIPAA, and GDPR compliance evidence packages
- Security scanning automation integrated into your daily development workflow
The Compliance-as-Code Workflow
Section titled “The Compliance-as-Code Workflow”The core idea: encode your regulatory requirements as executable rules that run automatically. Instead of proving compliance retroactively, you prevent non-compliance from shipping.
-
Define your compliance requirements as machine-readable policies. Translate each control (SOC 2 CC6.1, HIPAA 164.312, GDPR Article 25) into a rule that can be checked programmatically.
-
Implement enforcement at multiple layers. Pre-commit hooks catch issues locally. CI/CD gates block non-compliant code from merging. Infrastructure-as-code scanners validate deployment configurations.
-
Generate audit evidence automatically. Every enforcement check produces a timestamped log entry. These logs become your audit trail.
-
Produce compliance reports on demand. AI aggregates enforcement logs, Git history, and infrastructure state into narrative reports that auditors expect.
Policy Enforcement in CI/CD
Section titled “Policy Enforcement in CI/CD”The most reliable compliance enforcement happens in your CI/CD pipeline. Code that violates a policy never reaches production because the pipeline rejects it.
Generate the pipeline in agent mode, then use Cursor’s checkpoint and diff review to vet every generated rule. Compliance rules are exact-match by nature, so the visual review step matters more here than in most workflows.
@codebase "Create a GitHub Actions workflow that enforces the following compliancepolicies before any code can be merged to main:
1. All secrets must be stored in environment variables, never hardcoded (scan for patterns like API keys, passwords, tokens)2. All database queries must use parameterized statements (no string concatenation in SQL)3. All user-facing endpoints must have authentication middleware4. All PII fields must be encrypted at rest (check schema definitions)5. All changes to auth-related files require two approvals
For each check, log the result to a compliance-evidence.json artifactwith timestamp, check name, result (pass/fail), and affected files.Generate the workflow file and any helper scripts needed."Before accepting the diff, set a Cursor checkpoint and walk each generated rule against your actual control language. A regex that is slightly too broad will flag every PR; one that is too narrow silently passes violations. The checkpoint lets you revert a single bad rule without regenerating the whole workflow.
Claude Code’s edge here is that the same agent runs both interactively and headless, so the rules you generate locally are the rules that run in CI. Build them once, then invoke Claude headless inside the compliance job itself.
claude "Create a compliance enforcement system for our CI/CD pipeline.
Requirements:- GitHub Actions workflow that runs on every PR to main- Secret detection: scan for hardcoded API keys, passwords, and tokens- SQL injection prevention: verify parameterized queries- Authentication checks: ensure all public endpoints use auth middleware- PII protection: validate encryption on sensitive database fields- Approval gates: require 2 reviewers for changes to auth/ and security/ dirs
Each check should produce structured JSON output with: { timestamp, check_name, result, affected_files, evidence_hash }
Store results as workflow artifacts for audit trail.Generate all files needed: workflow YAML, scanning scripts, and acompliance-report-generator that summarizes results."Then run the analysis step itself in headless mode from the workflow, so the JSON evidence is produced by the same agent and parsed deterministically:
- name: Compliance review (headless) run: | claude -p "Review the diff in this PR against the rules in .compliance/policies.md. Emit one JSON object per violation: { control_id, file, line, severity, fix }. Emit [] if clean." \ --output-format json --allowedTools "Read,Grep,Bash" \ > compliance-evidence/review-$(date +%Y%m%d-%H%M%S).json--output-format json gives you a machine-parseable result you can gate the job on, and the timestamped artifact becomes audit evidence on its own.
Codex’s differentiator is the cloud task plus GitHub integration: the enforcement workflow can be authored and shipped as a PR without anyone running it locally. Open a cloud task that does the work and opens the PR for you.
From the terminal, kick off a cloud task and pull the diff back when it is done:
codex cloud exec --env prod-ci "Add a GitHub Actions compliance gate that runs onevery PR to main: secret scanning, parameterized-query check, auth-middlewarecheck on public endpoints, PII-encryption check on schemas, and a 2-reviewerrequirement for auth/ and security/. Emit structured JSON evidence per check andupload it as a workflow artifact. Open a PR with the workflow and scripts."codex apply # apply the task's diff to your local working tree to reviewOr skip the terminal entirely: comment @codex add the compliance gate described in our SOC 2 checklist on a tracking PR and Codex starts a cloud task using that PR as context, then pushes its changes back. Because the work lands as a reviewable PR, the generation of your compliance rules is itself captured in the change-management audit trail.
Pre-Commit Compliance Hooks
Section titled “Pre-Commit Compliance Hooks”Pre-commit hooks give developers immediate feedback before code ever leaves their machine. This is faster than waiting for CI and reduces compliance violations at the source.
Generate the standard .pre-commit-config.yaml, then use agent mode to add the project-specific rules and review each one in the diff before committing.
"Generate a pre-commit hook configuration (.pre-commit-config.yaml) thatenforces these compliance rules locally:
1. No secrets in committed files (use detect-secrets or gitleaks)2. No TODO/FIXME comments in files under src/security/3. All TypeScript files must have 'use strict' or strict mode enabled4. Database migration files must include a rollback step5. API route files must import from the auth middleware module
For each rule that fails, print a clear message explaining the violationand how to fix it. Also create a COMPLIANCE.md that documents each hookand the regulatory requirement it addresses."This is the portable, language-agnostic layer (the pre-commit framework) that every contributor gets regardless of which editor they use.
Beyond the shared .pre-commit-config.yaml, Claude Code has its own native hooks that fire around the agent’s own actions. Use a PreToolUse hook so the agent itself is blocked from writing a secret or an unauthenticated route, not just blocked at commit time.
claude "Add a Claude Code PreToolUse hook in .claude/settings.json that runson Edit and Write. The hook should run scripts/compliance/guard.sh on thefile being written and exit non-zero (blocking the edit) if it detects ahardcoded secret or a new API route missing the auth middleware import.Print the violated control ID so the block message is audit-friendly.Also generate the shared .pre-commit-config.yaml for git-level enforcement."The pre-commit config catches what a human commits; the Claude Code hook catches what the agent tries to write in the first place. Both write the same control IDs, so violations are traceable to a specific SOC 2 / HIPAA clause no matter which layer fires.
Treat the hook config as a small, reviewable change you can ship without local setup. Mention @codex on a tracking issue or PR and let the cloud task open the PR:
@codex add pre-commit compliance hooks: gitleaks for secret detection, acustom hook blocking TODO/FIXME in src/security/, a hook requiring a rollbackblock in every DB migration, and a hook checking API routes import the authmiddleware. Add .pre-commit-config.yaml plus the custom scripts and open a PR.Because it runs as a cloud task and lands as a PR, the change is reviewed and merged through the same approval flow as any code, so adding the guardrail is itself logged as a change-management event.
Automated Audit Trail Generation
Section titled “Automated Audit Trail Generation”Auditors need evidence that controls were enforced over time, not just at the moment of the audit. An automated audit trail captures enforcement data continuously.
Git-Based Audit Trail
Section titled “Git-Based Audit Trail”Your Git history already contains a rich audit trail. AI can extract compliance-relevant events from it.
Infrastructure Audit Trail
Section titled “Infrastructure Audit Trail”Infrastructure changes also need audit trails. If you use Terraform, CloudFormation, or Pulumi, every change is already versioned.
Run this interactively in agent mode when you are preparing for an audit window and want to read the findings as they come, then drill into specific Terraform diffs in the editor.
@codebase "Generate an infrastructure compliance report by analyzing:
1. All Terraform state changes in the past quarter2. Security group modifications (who changed what, when)3. IAM policy changes and their justifications from PR descriptions4. Encryption configuration status for all data stores5. Network access control changes
Cross-reference each change against our SOC 2 CC6.1 (logical access)and CC6.6 (boundary protection / network controls) requirements. Flagany changes that lack a corresponding approval in the PR process.
Output as a structured report with evidence links to specific commits."Cursor links each finding to the commit, so you can open the diff inline and eyeball whether the change really matches its PR justification before the report goes to the auditor.
Auditors want evidence collected continuously, not the night before. Claude Code’s headless mode lets you run this same audit on a schedule and drop the report straight into your evidence bucket — no human in the loop.
# scripts/compliance/infra-audit.sh — run from a weekly cron / scheduled CI jobclaude -p "Generate an infrastructure compliance audit report.Analyze Terraform state changes over the past 7 days. For each change extract:resource modified, Git author, PR number and approval status, and whether ittouches a security control (security groups, IAM, encryption, network ACLs).Map each finding to SOC 2 controls CC6.1 (logical access), CC6.6(boundary/network controls), and CC7.1 (detection of configuration changesand unauthorized components). Flag changes without proper PR approval." \ --output-format json --allowedTools "Read,Grep,Bash" \ > compliance-evidence/infra-audit-$(date +%Y%m%d).jsonBecause it is headless and emits JSON, you get a dated, tamper-evident artifact every week, and a downstream step can fail the job (or page the compliance channel) the moment an unapproved IAM change shows up.
Codex fits when the audit should live in the cloud and post itself to the team. Schedule a cloud task, or wire the GitHub Action so every infra PR gets an inline compliance review.
on: pull_request: paths: ['infra/**', '**/*.tf']jobs: audit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 - uses: openai/codex-action@v1 with: prompt-file: .github/codex/prompts/infra-audit.mdKeep the prompt in .github/codex/prompts/infra-audit.md (the same Terraform/IAM/encryption checks mapped to CC6.1, CC6.6, CC7.1). For an ad-hoc run, comment @codex audit the infra changes in this PR against our SOC 2 controls and Codex posts the findings as a PR review.
SOC 2 Evidence Package Generation
Section titled “SOC 2 Evidence Package Generation”SOC 2 Type II audits require evidence that controls operated effectively over a period (usually 6-12 months). Generating this evidence manually is the most time-consuming part of the audit.
Automating Evidence Collection
Section titled “Automating Evidence Collection”Assemble the package interactively so you can correct scope as the agent works — open the identity-provider export or a Terraform file inline and confirm the evidence matches before it lands in the document.
"Generate a complete SOC 2 Type II evidence package for the past quarter.
For each Trust Service Criteria, collect the following:
CC6.1 (Logical Access Controls):- User access reviews (export from our identity provider)- Role-based access control documentation- MFA enforcement evidence (percentage of users with MFA enabled)- Privileged access inventory
CC6.2 (System Account Management):- Service account inventory with last rotation dates- Automated credential rotation evidence from CI/CD logs- Account lifecycle documentation (creation, modification, termination)
CC6.6 (Boundary Protection) and CC6.7 (Data Classification / Transmission):- Network architecture diagram (generate from Terraform)- Data classification matrix- Data flow diagrams for PII (encryption in transit)
CC8.1 (Change Management):- All PRs merged to main with approval status- CI/CD pipeline pass rates- Deployment logs with rollback instances- Change advisory board meeting notes (if applicable)
For each control, provide:1. Control description2. Evidence collected3. Testing results (effective/deficiency)4. Recommendations for improvement
Output as a structured document that an auditor can review directly."A full SOC 2 package spans several independent evidence domains, which maps cleanly onto sub-agents: ask Claude Code to fan each control family out to its own sub-agent so they collect in parallel and report back to a single coordinator.
claude "Generate a SOC 2 Type II evidence package for Q4. Use a separatesub-agent per control family so they run in parallel, then merge the results:
- Sub-agent A — CC6.1: logical access controls (user reviews, RBAC, MFA)- Sub-agent B — CC6.2: system accounts (service account inventory, rotation)- Sub-agent C — CC6.6/CC6.7: boundary protection and data classification (network diagrams, data-flow/PII encryption)- Sub-agent D — CC8.1: change management (PR approvals, CI/CD logs, deployments)
Each sub-agent: describe the evidence needed, pull what is available from Githistory and CI/CD artifacts, document gaps requiring manual evidence, and rateeffectiveness with any deficiencies. The coordinator merges everything into oneauditor-ready document with an executive summary."Sub-agents keep each control’s context window focused on its own evidence and cut wall-clock time on a package that would otherwise be one long serial pass.
Evidence collection is long-running and best handled async. Submit it as a cloud task that compiles the package and opens a PR adding it under compliance/evidence/, then review the diff locally.
codex cloud exec --env compliance "Generate a SOC 2 Type II evidence packagecovering CC6.1, CC6.2, CC6.6/CC6.7, and CC8.1. Pull evidence from Git history,CI/CD artifacts, and infrastructure configs. Flag gaps that require manualevidence. Write the result to compliance/evidence/soc2-q4.md and open a PR."codex apply # bring the task's diff into your working tree to reviewRunning it in the cloud means the package regenerates on the same cadence each quarter without tying up a local session, and landing it as a PR puts the evidence itself under change control.
HIPAA Compliance Patterns
Section titled “HIPAA Compliance Patterns”HIPAA compliance requires specific technical safeguards for protected health information (PHI). AI tools can enforce these safeguards in code.
PHI Protection in Code
Section titled “PHI Protection in Code”// Example: AI-generated PHI access logging middlewareimport { auditLogger } from '~/lib/compliance/audit';import { classifyData } from '~/lib/compliance/data-classification';
export async function phiAccessMiddleware(request: Request, next: Function) { const classification = await classifyData(request);
if (classification.containsPHI) { await auditLogger.log({ timestamp: new Date().toISOString(), userId: request.auth.userId, action: request.method, resource: request.url, dataClassification: 'PHI', justification: request.headers.get('X-Access-Justification'), ipAddress: request.headers.get('X-Forwarded-For'), }); }
return next(request);}GDPR Compliance Automation
Section titled “GDPR Compliance Automation”GDPR requires specific capabilities: data subject access requests (DSARs), right to erasure, consent management, and data processing records.
Automating Data Subject Requests
Section titled “Automating Data Subject Requests”Build the handler in agent mode and set a checkpoint before accepting each data-store adapter — the erasure path is destructive, so you want to review the delete/anonymize logic in the diff rather than trust it blind.
"Generate a DSAR (Data Subject Access Request) handler that:
1. Accepts a user identifier (email or user ID)2. Searches all data stores for records associated with that user: - PostgreSQL (users, orders, payments, support_tickets) - Redis cache (session data, preferences) - S3 (uploaded documents, profile images) - Analytics events (PostHog) - Email service (Resend delivery logs)3. Compiles all data into a structured JSON export4. Generates a human-readable PDF summary5. Logs the DSAR fulfillment for our processing records6. Can also handle right-to-erasure by deleting/anonymizing all records
Include proper error handling for partial failures (e.g., one data storeis unreachable). The response should indicate which sources weresuccessfully queried and which failed, so we can retry."Generate the handler with tests, then lean on headless mode to keep the DSAR coverage honest over time — a scheduled claude -p check that proves every data store still has an adapter is itself GDPR Article 30 evidence.
claude "Build a GDPR DSAR handler.
Given a user email or ID, it should:- Query all data stores (PostgreSQL, Redis, S3, analytics, email logs)- Compile user data into JSON export- Generate PDF summary for the data subject- Log the request for Article 30 processing records- Support right-to-erasure (delete/anonymize across all stores)- Handle partial failures gracefully
Create the handler, data store adapters, and the API endpoint.Include tests for the happy path and partial failure scenarios."Then add a coverage gate to CI that fails when a data store has no adapter:
claude -p "List every persistent data store referenced in the codebase(DB connections, Redis clients, S3 buckets, external logging clients). Foreach, state whether a DSAR adapter exists. Emit JSON: { store, has_adapter }." \ --output-format json --allowedTools "Read,Grep"A DSAR handler spans several adapters and is a natural async build. Submit it as a cloud task that opens a PR, and keep the prompt in the repo so the work is reproducible and reviewable.
@codex build a GDPR DSAR handler that queries PostgreSQL, Redis, S3, andanalytics for all of a user's data. Support data export (JSON + PDF) andright-to-erasure across every store, log all requests for Article 30 records,and handle partial failures with retry. Add tests and open a PR.Mentioning @codex on a tracking issue starts a cloud task with that issue as context and pushes the handler back as a PR — so the erasure logic gets human review before it can ever run against production data.
Security Scanning Automation
Section titled “Security Scanning Automation”Continuous security scanning catches vulnerabilities before they reach production. The key is integrating scans into workflows developers already use, not adding separate security gates they learn to bypass.
Dependency Vulnerability Scanning
Section titled “Dependency Vulnerability Scanning”name: Compliance Security Scanon: pull_request: branches: [main] schedule: - cron: '0 6 * * 1' # Weekly Monday scan
jobs: dependency-scan: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Run dependency audit run: npm audit --json > audit-results.json - name: Analyze results run: | node scripts/compliance/analyze-audit.js \ --input audit-results.json \ --severity high,critical \ --output compliance-evidence/dependency-scan-$(date +%Y%m%d).json - name: Upload compliance evidence uses: actions/upload-artifact@v4 with: name: compliance-evidence-deps path: compliance-evidence/ retention-days: 365Static Analysis for Compliance
Section titled “Static Analysis for Compliance”Author the ESLint rules in agent mode and test them against real files in the editor — paste a known-bad logging statement and confirm the rule actually fires before you trust it in CI.
"Create a static analysis configuration that checks for compliance-relevantpatterns in our codebase:
1. Data handling: PII must be logged with redaction, never raw values2. Authentication: All API endpoints must check auth before processing3. Encryption: Sensitive config values must use encrypted env vars4. Logging: Security events must use the structured audit logger5. Error handling: Error responses must not leak internal details
Use ESLint custom rules where possible. For checks ESLint cannot handle,create standalone analysis scripts. Each rule should reference thespecific compliance requirement it enforces (e.g., SOC2-CC6.1, HIPAA-164.312)."Some compliance patterns (“does this error response leak a stack trace?”) are semantic, not syntactic, and a static linter can’t see them. Use Claude Code headless as a semantic checker in CI alongside ESLint, emitting the same control-tagged JSON.
claude "Create compliance-focused static analysis rules.
Rules needed:- PII redaction in logs (no raw email, SSN, phone in log statements)- Auth middleware on all API routes- Encrypted env vars for sensitive config- Structured audit logging for security events- No internal details in error responses
Implement as ESLint custom rules where possible, standalone scriptsfor the rest. Tag each rule with its compliance requirement ID."For the rules ESLint can’t express, add a headless semantic pass to the PR job:
claude -p "Review changed files for compliance violations ESLint cannot catch:error responses leaking internal details, log statements with unredacted PII,endpoints missing an auth check. Emit JSON: { control_id, file, line, issue }." \ --output-format json --allowedTools "Read,Grep"Run the static-analysis pass as part of the Codex GitHub Action so every PR is checked in CI and findings post back as a review, with the prompt versioned in the repo.
# add to .github/workflows/compliance-security.yml- uses: openai/codex-action@v1 with: prompt-file: .github/codex/prompts/compliance-lint.mdKeep .github/codex/prompts/compliance-lint.md tagging each finding with its requirement ID (SOC2-CC6.1, HIPAA-164.312). For an ad-hoc scan, @codex review for compliance regressions on the PR runs the same checks as a cloud task.
Regulatory Reporting Automation
Section titled “Regulatory Reporting Automation”Scheduled Compliance Reports
Section titled “Scheduled Compliance Reports”Automate weekly and quarterly compliance reports so the data is always current.
import { getGitActivity } from './sources/git';import { getCIResults } from './sources/ci';import { getSecurityScans } from './sources/security';import { getAccessReviews } from './sources/access';import { renderReport } from './templates/weekly';
async function generateWeeklyComplianceReport() { const period = { start: sevenDaysAgo(), end: now() };
const data = { gitActivity: await getGitActivity(period), ciResults: await getCIResults(period), securityScans: await getSecurityScans(period), accessReviews: await getAccessReviews(period), };
const report = renderReport({ ...data, controls: mapToControls(data), deficiencies: findDeficiencies(data), recommendations: generateRecommendations(data), });
await saveReport(report, `weekly-${period.end.toISOString()}`); await notifyComplianceTeam(report.summary);}When This Breaks
Section titled “When This Breaks”Compliance rules produce false positives. A secret scanner flags a test fixture containing a fake API key. Add an allowlist for known false positives, but review the allowlist quarterly to ensure it has not grown to hide real issues.
Audit trail has gaps. If developers push directly to main bypassing the PR process, the audit trail misses approvals. Enforce branch protection rules at the repository level, not just in CI. GitHub and GitLab both support requiring PR approvals as a repository setting.
DSAR handler misses a data store. When a new service is added, someone forgets to add it to the DSAR handler. Include a “data store registry” that all new services must register with, and add a CI check that verifies every database connection in the codebase has a corresponding DSAR adapter.
Compliance reports reference stale policies. The compliance documentation says you rotate credentials every 90 days, but the actual rotation script runs every 180 days. Automate the policy verification: the report generator should check actual rotation dates against the stated policy and flag discrepancies.
Pre-commit hooks slow down developers. If compliance hooks take more than 5 seconds, developers will skip them with --no-verify. Keep pre-commit checks fast (secret scanning, lint rules) and move slower checks (full dependency audit, infrastructure scanning) to CI.
Regulatory requirements change. When a regulation updates (such as new GDPR guidance or SOC 2 criteria revisions), your encoded policies need to change too. Subscribe to regulatory update feeds and schedule quarterly reviews of your compliance-as-code rules against current requirements.