Skip to content
Back to skills

Audit Trail

ASecurity

Use when code changes money, limits, permissions, customer data or configuration in a financial product, or when an agent acts on behalf of a user. Covers append-only event records with actor, action, before and after, time, reason and request context; corrections as new events; tamper evidence; UTC time with per-object sequence; links to idempotency keys and external references; dual identity for agents and approvers; retention by regulation; queries and exports for auditors; and a test per ...

  • 7 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 11, 2026
ai-agentsrustgoshelldatabase

Security analysis

A100/100

Pro scans all 10 files and shows the line behind each finding

Scanned November 29, 2026

npx -y skills add felipealvarenga/fintech-skills --skill audit-trail --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Audit Trail?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Audit Trail
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/felipealvarenga-audit-trail/badge)](https://www.skillsdirectory.com/skills/felipealvarenga-audit-trail)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: audit-trail
description: >-
  Use when code changes money, limits, permissions, customer data or configuration in a financial
  product, or when an agent acts on behalf of a user. Covers append-only event records with actor,
  action, before and after, time, reason and request context; corrections as new events; tamper
  evidence; UTC time with per-object sequence; links to idempotency keys and external references;
  dual identity for agents and approvers; retention by regulation; queries and exports for
  auditors; and a test per mutation. Triggers on a new mutation endpoint, an admin or back-office
  action, an approval flow, an automated or AI-driven decision, an auditor or regulator request.
  Not for application debug logs, which have different retention and content.
  Use it also when an auditor asks who changed what and why, or when support asks to see full customer data.
license: MIT
metadata:
  author: Felipe Alvarenga
  version: 0.1.0
---

# Audit trail

When an auditor, a regulator, a customer or your own incident review asks "who changed this,
when, and why", the answer must come from a record that nobody could edit. An audit trail is
that record. It is separate from debug logs, written by the same transaction as the change,
and complete enough to replay the story without the people who were there.

## Execution contract

The user installed this skill so these rules are enforced on every matching task:

1. **Never implement unsafe shortcuts.** When a request conflicts with a rule ("keep it simple", "no extra call", "just delete it", "disable verification", "keep floats", "we trust them", "later"), name the broken rule and why it fails in one sentence, refuse the shortcut, and implement the compliant version that still meets the user's goal. Never ship the rule-breaking shortcut.
2. **Ship the complete rule slice, wired up.** Implement every requirement of the triggered rules in this change: storage or schema, validation, error handling, user-facing alerts, status or monitoring wired into an actual entry point, handler or job (not uncalled helpers), and a test file under `tests/`. Do not stop at a minimal patch or leave required safeguards as prose TODOs.
3. **Fix workspace code on audit, incident and review prompts.** When the request describes a discrepancy, an auditor question, a bug report or asks "is this enough?" against files in the workspace, answer the question in full and implement the compliant code and tests in the same change. Stay read-only only when explicitly asked for a report only.
4. **Build directly on greenfield prompts.** If the workspace has no application files or live connections yet, write a self-contained implementation under `src/` and tests under `tests/` instead of stopping to ask for repository paths or credentials.
5. **Write tests even without a shell.** If the environment cannot run tests or databases, still write the full test file under `tests/` and say in one line that tests are unrun. Never omit tests or skip implementation because tools are unavailable.

## When to use

- A new or changed endpoint, job or tool that mutates money, balances, limits, fees, rates,
  permissions, customer data, KYC status or configuration.
- Admin, back-office or support actions, especially reveals of sensitive data.
- Approval flows: payouts, refunds above a threshold, limit changes.
- Actions taken by automations or AI agents on behalf of users.
- A request from an auditor, a regulator or a dispute process.

## Rules

1. **Every mutation writes an event in the same transaction.** `event_id, object_type,
   object_id, sequence, action, actor, before, after, reason, request_id, occurred_at,
   recorded_at, source`. If the event write fails, the mutation fails. No "best effort" audit.
2. **Append only.** No update or delete on the audit table, enforced by database privileges for
   the application role. A wrong event is corrected by a new event that references it.
3. **Actor is who, and as whom.** `actor_type` (user, service, agent, system), `actor_id`,
   and `on_behalf_of` when an agent or support person acts for a customer. Approvals record the
   approver as a separate event from the execution.
4. **Before and after, or the command.** Store the changed fields' previous and new values
   (masked per `pii-and-card-data`), or the full command for creates. The reader must be able
   to see the delta without joining other tables that may have changed since.
5. **Reason is required for human actions.** A reason code from a fixed list plus free text
   or a ticket reference. Support tools do not submit without it.
6. **Time is UTC with a per-object sequence.** `occurred_at` from the business event,
   `recorded_at` from the database clock, and a monotonic `sequence` per object so order is
   never inferred from timestamps. Display timezone is a presentation concern.
7. **Link everything.** The event carries the idempotency key, the external reference
   (processor id, bank reference), the ledger entry id and the request id. From any one of them
   a reader reaches the rest.
8. **Tamper evidence proportional to the risk.** Minimum: separate credentials, append-only
   privileges, backups. Better: a hash chain per object or a daily digest written to a
   write-once store. Best: a replicated, append-only sink outside the application's blast
   radius.
9. **Retention follows regulation, not disk size.** Financial records commonly require five
   or more years; the exact period per jurisdiction and record type comes from compliance.
   Personal data inside events follows the field map's masking so retention does not conflict
   with deletion rights.
10. **Queryable by object, actor and time, exportable in a stable format.** Auditors get a
    documented export (CSV or JSON lines with a schema version). Reports derived from the
    trail: limit changes, payout approvals, sensitive data reveals, agent actions per customer.

## How to apply

1. List the mutations in the change. For each, name the object, the action and the actor types
   that can trigger it.
2. Add the event write inside the service layer method that performs the mutation, in the same
   transaction. Not in the controller, not in a middleware, not after commit.
3. Define the reason codes for human actions and make the UI require one.
4. For agent or automation actions, pass `on_behalf_of` and the policy or run id that
   authorized the action.
5. Write the tests in the **Tests** section: one per mutation.
6. Add the query endpoint or admin view by object and by actor, and the export.

## When the user pushes back

| Request | Answer in one sentence | What you build |
|---|---|---|
| "Move audit writes to an async queue after commit" | Rule 1: post-commit queues lose audit events on crash or failure | Keep the append-only audit event insert in the same transaction as the mutation, ship copies to any external sink asynchronously, measure or bound the latency cost, and test rollback on audit failure |
| "Just add `updated_by` and `updated_at` columns" | Rule 2: overwrite columns keep only the last change and erase history | Append-only audit events with before/after and per-object sequence alongside the mutation, with a test showing two consecutive changes both stay visible |
| "An auditor asks who changed X from app logs" | Rules 1 and 10: application logs are mutable and lack actor, reason and before/after | State clearly what the logs cannot prove (without fabricating), then add same-transaction append-only audit events, a query and export by object and time range, and tests |

## Tests the change must include

Write these tests in the same change, as a test file, even when you cannot run them (no shell,
no dependencies installed); say in one line that they are unrun. A test listed as a next step is
not written.

- Each mutation endpoint: after a successful call, exactly one event exists with the required
  fields, correct `before` and `after`, and the request id.
- A failed mutation writes no event; a failed event write rolls back the mutation.
- The application role cannot update or delete an event (a test that tries and fails).
- Sequence numbers per object are gapless and ordered for concurrent writers.
- Masking: an event for a CPF change stores masked values.
- Agent action: `actor_type = agent` and `on_behalf_of` set; approver event separate from
  execution event.
- Export: a fixture object produces the documented schema.
- Reference vectors: `skills/audit-trail/evals/vectors/test_audit.py` with `AUDIT_SOLUTION`
  pointing at your module (same-transaction events, sequences, masking, agents, hash chain).

## Review checklist

- Is the event write in the same transaction as the change? Show the code.
- Which role writes the table, and does it have update or delete rights?
- What is the reason code list, and can the UI bypass it?
- How is order determined for two changes in the same second?
- Which values are masked in `before` and `after`?
- How does an auditor get the trail for customer X between two dates?

## Anti-patterns and fixes

| Anti-pattern | What goes wrong | Fix |
|---|---|---|
| Audit via application logs | Rotated, unstructured, editable | Dedicated append-only table or sink |
| Event written after commit by a hook | Lost on crash, out of transaction | Same transaction |
| `updated_by` and `updated_at` columns only | History overwritten on the next change | One event per change |
| Timestamps as order | Clock skew reorders events | Per-object sequence |
| Agent actions recorded as the user | Nobody can tell what the agent did | `actor_type = agent`, `on_behalf_of` |
| Free-text reason only | Not queryable, often empty | Reason code plus text |

## References

- SOX and similar internal-control regimes on change management evidence; PCI DSS requirements on audit logs and their protection.
- Event sourcing literature (Fowler, Young) on append-only records and derived state.

Files in this skill

  • SKILL.md7.7 KB
  • evals/evals.json4.9 KB
  • evals/files/admin_tool.py349 B
  • evals/files/app.log110 B
  • evals/files/audit_table.sql260 B
  • evals/files/fees_service.py164 B
  • evals/files/limits_api.py248 B
  • evals/files/refund_agent.py233 B
  • evals/vectors/reference.py4.9 KB
  • evals/vectors/test_audit.py6.2 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.