audit-pattern

A set of rules for keeping a trustworthy record of changes and operations. The main audit record is saved in the database together with the business change, while event logs provide a derived copy for monitoring and tracing.

In plain words
What is it for?
Use it when designing audit tables, database transactions, event-log handlers, monitoring feeds, and tamper-evident operation histories.
Why use it?
It prevents audit records from disappearing when a business change is rolled back or when a separate event-log write fails. A hash chain in the database also helps reveal altered records.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/yudefine/nuxt-supabase-starter/audit-pattern
Clone the repo
git clone --depth 1 https://github.com/YuDefine/nuxt-supabase-starter

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 2,659 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.02659
Opus 5 $0.00000 $0.01329
Sonnet 5 $0.00000 $0.00532
Haiku 4.5 $0.00000 $0.00266

Measured 2d ago against content hash 4c13534ac4b7, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

audit-pattern scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

template/.cursor/rules/audit-pattern.mdc · 142 lines

How it starts

The opening of the file, as written. The whole thing — 142 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Audit Pattern

D-pattern audit 是 clade 對 audit / operation log 的標準治理模式。決策已定: 不採 DB-only、evlog-only、雙寫並行;新建與升級都走 DB transactional outbox canonical、evlog derived stream、DB hash anchor。

Reference: docs/d-pattern-master-plan.md

三段定義

  1. DB outbox canonical:audit row 與業務 mutation 必須在同一個 PostgreSQL transaction 內完成。business commit 成功時 audit row 必須存在;business rollback 時 audit row 也 rollback。
  2. evlog derived stream:evlog 只從 canonical audit event 衍生,用於 ops、security monitoring、cross-service trace、短 TTL PII envelope。handler 不能只送 fire-and-forget audit event 就宣稱 audit 完成。
  3. Hash anchor in DBprev_hashhash 直接寫在 canonical DB row。Cloudflare Workers 的 node:fs VFS 不是 durable journal,多 instance hash chain 也會 race,所以 hash chain 不依賴 fs journal。

Source-of-truth 規則:任何 audit 問題先查 DB row,evlog 是衍生視圖;evlog miss 是 monitoring 缺口,不改變 canonical audit truth。

MUST

  • Audit canonical truth MUST live in DB transactional outbox。
  • Audit row MUST 與業務 mutation 在同一個 PostgreSQL transaction 內完成;Supabase JS 多次 .from().insert() 不是 transaction,必要時用 SQL RPC。
  • evlog audit events MUSTauditEventId,且該值必須對應 DB canonical row 的 event_id
  • DB row MUST 包含 prev_hash / hash
  • DB row MUST NOT 包含 ip_address / user_agent / device fingerprint 等 PII 欄位。
  • Multi-tenant consumer MUST 使用 per-tenant chain;實作可用 PostgreSQL advisory lock per tenant,或用 partition / tenant-scoped chain owner。
  • business_keys MUST 只放結構化業務鍵,例如 invoiceIdreportVersionpolicyVersionrowCount
  • business_keys MUST NOT 放 PII、姓名、email、raw LLM prompt、raw request body、大型 payload。
  • 拒絕操作(auth 失敗、role 失敗、policy deny、quota deny)MUST 呼叫 auditDeny()
  • requireAuth() / requireRole() / policy helper 若會拒絕使用者,失敗路徑 MUST 自動寫入 auditDeny(),不可要求每個 handler 手寫。
  • Audit drain reliability MUST 透過 Postgres outbox dispatcher(claim_audit_outbox_batch + FOR UPDATE SKIP LOCKED + idempotent mark_audit_evlog_drained);MUST NOT 依 tenant tier 或事件等級降級成 fire-and-forget evlog。Drain 端 MUSTauditEventId idempotently 去重(at-least-once 語意)。
  • Atomicity 依事件 風險等級 分流,不依商業 tier:高風險 mutation(refund / billing、role / permission、data export、regulated report finalization、AI agent autonomous decision / tool invoke)MUST 用 SQL RPC 包 business mutation + audit insert 同 transaction;一般 CUD 用 helper baseline,但 audit insert MUST 緊接 business write,MUST NOT 先 response 或丟 background promise。
  • Outbox dispatcher state MUST 放在 Postgres(evlog_drained_at / attempts / next_attempt_at / lease_until),MUST NOT 用 KV / D1 / Worker memory 作 durability source。LISTEN/NOTIFY、Realtime、Worker memory buffer 只能作 wake-up / best-effort signal,MUST NOT 被當成 durable queue。

Read the full file on GitHub · 142 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 142 lines · 0 tokens per session scan A 4c13534ac4b7

Subscribe to this mod's changes

audit-pattern is a cursor rule published in the GitHub repository YuDefine/nuxt-supabase-starter (45 stars, last pushed 4d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,659 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.