papertrail/db/migrations/0056_evidence-audit.sql
caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

49 lines
2.3 KiB
SQL

-- Tamper-evident EVIDENCE AUDIT CHAIN. A per-org, hash-chained, append-only log
-- of evidence-bearing actions (dossier built, claim verified, source accessed,
-- approval signed) for 21 CFR Part 11-grade defensibility.
--
-- This is deliberately NOT the general audit_log table (0001_foundation.sql).
-- The general log is a flat, best-effort review trail; this is a VERIFIABLE
-- chain. Each row carries prev_hash + hash where
-- hash = sha256(prev_hash + canonical(entry))
-- so any retroactive edit, deletion, or reordering of a row breaks every hash
-- downstream of it and is detectable by recomputation.
--
-- * seq — dense, per-org monotonic sequence (1, 2, 3, ...). unique per
-- org so the chain has a single, gap-checkable order.
-- * action — evidence action verb (e.g. 'dossier.built', 'claim.verified')
-- * entity_* — what the action was about; entity_id is text (soft ref)
-- * actor — the acting user (nullable: system/automated actions)
-- * payload — the canonicalized, hashed content of the entry
-- * prev_hash — hash of the previous link ('' genesis for seq 1)
-- * hash — this link's hash; the chain's tamper-evidence anchor
--
-- Idempotent: safe to run repeatedly. org_id on the table; uuid pk; created_at.
-- actor is a soft reference (nullable, no FK) so a chain link outlives the user
-- that produced it — a signed approval record must never vanish because an
-- account was deleted.
create table if not exists evidence_audit_chain (
id uuid primary key default gen_random_uuid(),
org_id uuid not null references orgs(id) on delete cascade,
seq bigint not null,
action text not null,
entity_type text not null,
entity_id text,
actor uuid,
payload jsonb not null default '{}'::jsonb,
prev_hash text not null,
hash text not null,
created_at timestamptz not null default now(),
unique (org_id, seq)
);
-- Primary read path: an org's chain in order, newest first for listing and
-- oldest first for verification (both served by this composite index).
create index if not exists evidence_audit_chain_org_seq_idx
on evidence_audit_chain(org_id, seq desc);
-- Lookups by the entity an action concerned (e.g. all events for a dossier).
create index if not exists evidence_audit_chain_org_entity_idx
on evidence_audit_chain(org_id, entity_type, entity_id);