Compliance

From Pageloom/weft-id · View interactive version

Compliance is an AI agent skill from the GitHub repository Pageloom/weft-id. Compliance Agent - Verify architectural principles and design patterns

How to use this skill

Skill file

Compliance Agent - Architectural Enforcement Mode

Verify the codebase adheres to architectural principles and design patterns.

Quick Reference

Before You Start

Read .claude/THOUGHT_ERRORS.md to avoid past mistakes.

Principles

1. Activity Tracking & Event Logging (PRIMARY)

Rule: "If there is a write, there is a log" - NO EXCEPTIONS

2. Tenant Isolation

All data access must be tenant-scoped via tenant_id parameter or UNSCOPED constant.

3. Authorization

4. Service Layer Architecture

Request → Router → Service → Database → PostgreSQL

5. API-First

All functionality achievable via RESTful API endpoints in app/routers/api/v1/.

Exceptions: Auth flows, SAML ACS/SLO, admin UI conveniences.

Documentation: API endpoint docstrings must accurately list all supported parameters and fields. When a PATCH/PUT endpoint accepts a schema, the docstring must document every field the schema exposes (not just a subset). Incomplete documentation misleads API consumers.

6. Input Length Validation

Rule: Every str field in Pydantic input schemas and every Form() str parameter must have max_length. Database TEXT columns must have matching constraints.

Standard limits: names/titles 255, descriptions 2000, URLs 2048, enum-like 50, subdomains 63, domains 253, IP addresses 45, passwords 255, emails 320, UUIDs/IDs 50, codes 100, timezone 50, locale 10.

8. Migration Backwards Compatibility

Rule: Migrations must be safe to apply on a running instance without breaking the application.

High severity (immediate breakage):

Medium severity (lock contention or partial breakage):

Safe patterns:

Suppression: Add -- migration-safety: ignore on its own line in a migration file to skip all safety checks for that file. Use this for intentional cleanup migrations where breaking changes have already been prepared by a prior code deploy.

10. Template Links

Rule: Template href and action attributes must point to routes that exist in the application.

11. Outbound Request Timeouts

Rule: All outbound HTTP/network calls must have explicit timeouts to prevent indefinite hangs.

12. Job System Context

Rule: Job handlers in app/jobs/ that call log_event() must use system_context().

7. RLS Policy Consistency

Rule: Every table with ENABLE ROW LEVEL SECURITY must have a correct policy.

Workflow

1. Run Automated Script First

make check                              # Full suite (lint, format, types, compliance)
python dev/compliance_check.py     # Compliance only

Compliance-only options:

--check architecture    # Router imports
--check activity        # Activity/event logging
--check tenant          # Tenant isolation
--check api-first       # API coverage + endpoint docstring completeness
--check authorization   # Route auth
--check input-length    # Pydantic str fields without max_length
--check sql-length      # SQL TEXT columns without length CHECK constraints
--check rls             # RLS policies: USING + WITH CHECK, current_setting(true)
--check migration-safety # Backwards compatibility of migration files
--check template-links       # Template href/action link validity
--check outbound-timeouts    # Outbound HTTP calls must have timeouts
--check job-context          # Job handlers must use system_context() for log_event()
--check api-auth             # All API routes must require authentication
--check form-input-length    # Form() str parameters must have max_length
--check template-xss         # innerHTML with interpolation must use escapeHtml()

2. Investigate Findings

3. Manual Review (if needed)

Focus on:

4. Log as GitHub issues

One issue per violation, labelled bug plus one severity-* label, with the issue format below as the body (its heading becomes the title). See .claude/references/issue-tracking.md for the commands. A violation that is an exploitable vulnerability (for example a tenant-isolation gap) goes to a private draft security advisory instead.

Event Context Note

Request context (IP, user agent, device, session) is handled automatically by RequestContextMiddleware. You do NOT need to check for explicit request_metadata passing.

Red Flags

| Pattern | Violation |

|---------|-----------|

| Service with RequestingUser but no track_activity() | Activity Logging |

| Mutation without log_event() | Activity Logging |

| log_event() before mutation | Activity Logging |

| SQL without tenant_id filter | Tenant Isolation |

| Router imports database | Architecture |

| Service operation without API endpoint | API-First |

| API docstring missing supported fields | API-First |

| str field without max_length in input schema | Input Validation |

| Field(default=None) without max_length | Input Validation |

| TEXT/CITEXT column without CHECK (length(...) <= N) | SQL Length Validation |

| RLS policy missing WITH CHECK clause | RLS Policy Consistency |

| current_setting() without true parameter | RLS Policy Consistency |

| DROP COLUMN / DROP TABLE in migration | Migration Safety |

| RENAME COLUMN / RENAME TABLE in migration | Migration Safety |

| ADD COLUMN NOT NULL without DEFAULT in migration | Migration Safety |

| ALTER COLUMN TYPE in migration | Migration Safety |

| CREATE INDEX without CONCURRENTLY in migration | Migration Safety |

| Template href/action not matching any route | Template Links |

| httpx.get/post(...) without timeout= | Outbound Timeouts |

| urllib.request.urlopen(...) without timeout= | Outbound Timeouts |

| smtplib.SMTP(...) without timeout= | Outbound Timeouts |

| SDK client constructor without timeout config | Outbound Timeouts |

| Form() str parameter without max_length | Form Input Length |

| innerHTML with ${...} without escapeHtml() | Template XSS Prevention |

| API handler without auth dependency | API Authentication |

See .claude/references/compliance-patterns.md for detailed patterns and checklists.

Issue Format

## [Principle Violated]: [Brief Description]

**Found in:** [File:line]
**Severity:** High
**Principle Violated:** [Activity Logging | Tenant Isolation | Authorization | Service Layer | API-First | Input Validation]
**Description:** [What's wrong]
**Evidence:** [Code snippet]
**Impact:** [Security, compliance, maintainability]
**Root Cause:** [Why this happened]
**Suggested fix:** [Specific code change]

Example:

Add after mutation at line 245:

log_event(

tenant_id=requesting_user["tenant_id"],

actor_user_id=requesting_user["id"],

event_type="user_inactivated",

artifact_type="user",

artifact_id=user_id,

)

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.