Schema Designed
The SQL for the Human Review service's tables, in the review schema. No other service can read them.
-- Human Review service, version 1. Schema: review.
-- IDs are UUIDv7 values generated by the service.
-- Checked with `npm run schema:check`: runs on PGlite, and every sample row passes every constraint.
create schema if not exists review;
create table review.tasks (
id uuid primary key,
owner_id uuid not null,
reference text not null,
status text not null default 'open',
checks_overridden boolean not null default false,
created_at timestamptz not null default now(),
completed_at timestamptz,
completed_by uuid,
held_by text,
released_at timestamptz,
constraint tasks_reference_check check (length(reference) between 1 and 200),
constraint tasks_held_by_check check (held_by ~ '^service:[a-z][a-z0-9-]*$'),
constraint tasks_released_check check (released_at is null or held_by is not null),
constraint tasks_status_check check (status in ('open', 'completed')),
constraint tasks_completed_check check (
(status = 'completed') = (completed_at is not null)
and (completed_at is null) = (completed_by is null)
and (completed_at is null or completed_at >= created_at)
)
);
create index tasks_owner_id_idx on review.tasks (owner_id, created_at desc);
create table review.fields (
id uuid primary key,
task_id uuid not null,
name text not null,
line_index smallint,
type text not null,
original_value jsonb,
value jsonb,
confidence numeric(3,2) not null,
source_page smallint,
source_quote text,
flagged boolean not null,
flag_reasons text[] not null default '{}',
status text not null default 'pending',
updated_by uuid,
updated_at timestamptz,
constraint fields_task_id_fkey foreign key (task_id)
references review.tasks (id) on delete cascade,
constraint fields_task_name_line_key unique nulls not distinct (task_id, name, line_index),
constraint fields_name_check check (length(name) between 1 and 64 and (line_index is null or line_index >= 0)),
constraint fields_type_check check (type in ('text', 'date', 'decimal', 'currency')),
constraint fields_status_check check (status in ('pending', 'accepted', 'edited', 'not_present')),
constraint fields_flagged_check check (flagged = (cardinality(flag_reasons) > 0)),
constraint fields_decided_check check (
(status <> 'pending') = (updated_by is not null)
and (updated_by is null) = (updated_at is null)
),
constraint fields_not_present_check check (
(status <> 'not_present' or value is null)
and (status <> 'accepted' or value is not distinct from original_value)
),
constraint fields_confidence_check check (confidence between 0 and 1 and (source_page is null or source_page >= 1)),
constraint fields_quote_check check (length(source_quote) between 1 and 500)
);
-- The queue: flagged first, then lowest confidence.
create index fields_queue_idx on review.fields (task_id, flagged desc, confidence);
create table review.field_changes (
id uuid primary key,
field_id uuid not null,
action text not null,
old_value jsonb,
new_value jsonb,
source text not null default 'review',
changed_by uuid not null,
created_at timestamptz not null default now(),
constraint field_changes_field_id_fkey foreign key (field_id)
references review.fields (id) on delete cascade,
constraint field_changes_action_check check (action in ('accept', 'edit', 'not_present')),
constraint field_changes_source_check check (source in ('review'))
);
create index field_changes_field_id_idx on review.field_changes (field_id, created_at);
create table review.eval_candidates (
id uuid primary key,
task_id uuid not null,
reference text not null,
field_name text not null,
line_index smallint,
model_value jsonb,
human_value jsonb,
status text not null default 'candidate',
decided_by uuid,
decided_at timestamptz,
created_at timestamptz not null default now(),
constraint eval_candidates_reference_check check (length(reference) between 1 and 200 and length(field_name) between 1 and 64),
constraint eval_candidates_task_id_fkey foreign key (task_id)
references review.tasks (id) on delete cascade,
constraint eval_candidates_status_check check (status in ('candidate', 'approved', 'rejected')),
constraint eval_candidates_changed_check check (human_value is distinct from model_value),
constraint eval_candidates_decided_check check (
(status <> 'candidate') = (decided_at is not null)
and (decided_at is null) = (decided_by is null)
)
);
-- One candidate per corrected field of a task; the admin list by status.
create unique index eval_candidates_task_field_idx
on review.eval_candidates (task_id, field_name, line_index) nulls not distinct;
create index eval_candidates_status_idx on review.eval_candidates (status, created_at);
create table review.inbound_events (
event_id uuid primary key,
type text not null,
created_at timestamptz not null default now(),
constraint inbound_events_type_check check (length(type) between 1 and 100)
);
-- The service's database login reaches only this schema. History is never edited.
grant usage on schema review to review_service;
grant select, insert, update, delete on review.tasks, review.fields to review_service;
grant select, insert, delete on review.field_changes to review_service;
grant select, insert, update, delete on review.eval_candidates to review_service;
grant select, insert on review.inbound_events to review_service;
grant select, insert on review.audit_events to review_service;
Schema level: enforced by the database, whatever the code does
- A field appears once per task and line item (nulls not distinct).
- A decided field always records who and when; an accepted field keeps its original value; a not-present field has none.
- A completed task always records who completed it and when.
- A candidate always differs from what the model said, and records who decided on it.
- One candidate per corrected field of a task.
- History rows are inserted and read, never updated. Review's database login can reach only the
review schema.
Constraints and indexes by table
review.tasks
tasks_pkey: primary key (id)tasks_status_check: open or completedtasks_completed_check: completed_at and completed_by are set exactly when completedtasks_reference_check: reference 1 to 200 characterstasks_held_by_check: held_by is like service:<name>tasks_released_check: released_at only when held_by is settasks_owner_id_idx: index on (owner_id, created_at desc), for a user's tasks
review.fields
fields_pkey: primary key (id)fields_task_id_fkey: foreign key (task_id) references review.tasks on delete cascadefields_task_name_line_key: unique (task_id, name, line_index) nulls not distinctfields_type_check, fields_status_check: only the listed valuesfields_flagged_check: flagged exactly when flag_reasons is not emptyfields_decided_check: updated_by and updated_at are set exactly when status is not pendingfields_not_present_check: a not_present field has no value; an accepted one keeps original_valuefields_confidence_check, fields_quote_check: confidence 0 to 1; quote 1 to 500 charactersfields_queue_idx: index on (task_id, flagged desc, confidence), for the queue order
review.field_changes
field_changes_pkey: primary key (id)field_changes_field_id_fkey: foreign key (field_id) references review.fields on delete cascadefield_changes_action_check, field_changes_source_check: only the listed valuesfield_changes_field_id_idx: index on (field_id, created_at), for a field's history in order
review.eval_candidates
eval_candidates_pkey: primary key (id)eval_candidates_status_check: only the listed valueseval_candidates_changed_check: human_value is different from model_valueeval_candidates_decided_check: decided_by and decided_at are set exactly when not candidateeval_candidates_task_id_fkey: foreign key to review.tasks, delete cascadeseval_candidates_task_field_idx: unique (task_id, field_name, line_index) nulls not distinct, one candidate per corrected fieldeval_candidates_status_idx: index on (status, created_at), for the admin list
review.inbound_events
inbound_events_pkey: primary key (event_id)inbound_events_type_check: 1 to 100 characters
Sample data
Common queries
The review queue (`GET /tasks/{taskId}`)
select f.*
from review.fields f
join review.tasks t on t.id = f.task_id
where f.task_id = $1 and t.owner_id = $2
and (f.flagged or $3) -- $3: all=true
order by f.flagged desc, f.confidence;
Can the task be completed?
select count(*) filter (where flagged and status = 'pending') as unresolved
from review.fields
where task_id = $1;
Record a correction as a candidate (in the same transaction as the edit)
insert into review.eval_candidates (id, task_id, reference, field_name, line_index, model_value, human_value)
values ($1, $2, $3, $4, $5, $6, $7)
on conflict (task_id, field_name, line_index)
do update set human_value = excluded.human_value;
Schema on GitHub →